annotate clang/lib/Sema/SemaTemplateVariadic.cpp @ 239:173fe712db74

merge LLVM16
author kono
date Wed, 09 Nov 2022 18:03:41 +0900
parents dd44ba33042e c4bab56944e8
children ca573705d418
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===------- SemaTemplateVariadic.cpp - C++ Variadic Templates ------------===/
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //===----------------------------------------------------------------------===/
anatofuz
parents:
diff changeset
7 //
anatofuz
parents:
diff changeset
8 // This file implements semantic analysis for C++0x variadic templates.
anatofuz
parents:
diff changeset
9 //===----------------------------------------------------------------------===/
anatofuz
parents:
diff changeset
10
anatofuz
parents:
diff changeset
11 #include "clang/Sema/Sema.h"
anatofuz
parents:
diff changeset
12 #include "TypeLocBuilder.h"
anatofuz
parents:
diff changeset
13 #include "clang/AST/Expr.h"
anatofuz
parents:
diff changeset
14 #include "clang/AST/RecursiveASTVisitor.h"
anatofuz
parents:
diff changeset
15 #include "clang/AST/TypeLoc.h"
anatofuz
parents:
diff changeset
16 #include "clang/Sema/Lookup.h"
anatofuz
parents:
diff changeset
17 #include "clang/Sema/ParsedTemplate.h"
anatofuz
parents:
diff changeset
18 #include "clang/Sema/ScopeInfo.h"
anatofuz
parents:
diff changeset
19 #include "clang/Sema/SemaInternal.h"
anatofuz
parents:
diff changeset
20 #include "clang/Sema/Template.h"
anatofuz
parents:
diff changeset
21
anatofuz
parents:
diff changeset
22 using namespace clang;
anatofuz
parents:
diff changeset
23
anatofuz
parents:
diff changeset
24 //----------------------------------------------------------------------------
anatofuz
parents:
diff changeset
25 // Visitor that collects unexpanded parameter packs
anatofuz
parents:
diff changeset
26 //----------------------------------------------------------------------------
anatofuz
parents:
diff changeset
27
anatofuz
parents:
diff changeset
28 namespace {
anatofuz
parents:
diff changeset
29 /// A class that collects unexpanded parameter packs.
anatofuz
parents:
diff changeset
30 class CollectUnexpandedParameterPacksVisitor :
anatofuz
parents:
diff changeset
31 public RecursiveASTVisitor<CollectUnexpandedParameterPacksVisitor>
anatofuz
parents:
diff changeset
32 {
anatofuz
parents:
diff changeset
33 typedef RecursiveASTVisitor<CollectUnexpandedParameterPacksVisitor>
anatofuz
parents:
diff changeset
34 inherited;
anatofuz
parents:
diff changeset
35
anatofuz
parents:
diff changeset
36 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded;
anatofuz
parents:
diff changeset
37
anatofuz
parents:
diff changeset
38 bool InLambda = false;
anatofuz
parents:
diff changeset
39 unsigned DepthLimit = (unsigned)-1;
anatofuz
parents:
diff changeset
40
anatofuz
parents:
diff changeset
41 void addUnexpanded(NamedDecl *ND, SourceLocation Loc = SourceLocation()) {
anatofuz
parents:
diff changeset
42 if (auto *VD = dyn_cast<VarDecl>(ND)) {
anatofuz
parents:
diff changeset
43 // For now, the only problematic case is a generic lambda's templated
anatofuz
parents:
diff changeset
44 // call operator, so we don't need to look for all the other ways we
anatofuz
parents:
diff changeset
45 // could have reached a dependent parameter pack.
anatofuz
parents:
diff changeset
46 auto *FD = dyn_cast<FunctionDecl>(VD->getDeclContext());
anatofuz
parents:
diff changeset
47 auto *FTD = FD ? FD->getDescribedFunctionTemplate() : nullptr;
anatofuz
parents:
diff changeset
48 if (FTD && FTD->getTemplateParameters()->getDepth() >= DepthLimit)
anatofuz
parents:
diff changeset
49 return;
anatofuz
parents:
diff changeset
50 } else if (getDepthAndIndex(ND).first >= DepthLimit)
anatofuz
parents:
diff changeset
51 return;
anatofuz
parents:
diff changeset
52
anatofuz
parents:
diff changeset
53 Unexpanded.push_back({ND, Loc});
anatofuz
parents:
diff changeset
54 }
anatofuz
parents:
diff changeset
55 void addUnexpanded(const TemplateTypeParmType *T,
anatofuz
parents:
diff changeset
56 SourceLocation Loc = SourceLocation()) {
anatofuz
parents:
diff changeset
57 if (T->getDepth() < DepthLimit)
anatofuz
parents:
diff changeset
58 Unexpanded.push_back({T, Loc});
anatofuz
parents:
diff changeset
59 }
anatofuz
parents:
diff changeset
60
anatofuz
parents:
diff changeset
61 public:
anatofuz
parents:
diff changeset
62 explicit CollectUnexpandedParameterPacksVisitor(
anatofuz
parents:
diff changeset
63 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded)
anatofuz
parents:
diff changeset
64 : Unexpanded(Unexpanded) {}
anatofuz
parents:
diff changeset
65
anatofuz
parents:
diff changeset
66 bool shouldWalkTypesOfTypeLocs() const { return false; }
anatofuz
parents:
diff changeset
67
anatofuz
parents:
diff changeset
68 //------------------------------------------------------------------------
anatofuz
parents:
diff changeset
69 // Recording occurrences of (unexpanded) parameter packs.
anatofuz
parents:
diff changeset
70 //------------------------------------------------------------------------
anatofuz
parents:
diff changeset
71
anatofuz
parents:
diff changeset
72 /// Record occurrences of template type parameter packs.
anatofuz
parents:
diff changeset
73 bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
anatofuz
parents:
diff changeset
74 if (TL.getTypePtr()->isParameterPack())
anatofuz
parents:
diff changeset
75 addUnexpanded(TL.getTypePtr(), TL.getNameLoc());
anatofuz
parents:
diff changeset
76 return true;
anatofuz
parents:
diff changeset
77 }
anatofuz
parents:
diff changeset
78
anatofuz
parents:
diff changeset
79 /// Record occurrences of template type parameter packs
anatofuz
parents:
diff changeset
80 /// when we don't have proper source-location information for
anatofuz
parents:
diff changeset
81 /// them.
anatofuz
parents:
diff changeset
82 ///
anatofuz
parents:
diff changeset
83 /// Ideally, this routine would never be used.
anatofuz
parents:
diff changeset
84 bool VisitTemplateTypeParmType(TemplateTypeParmType *T) {
anatofuz
parents:
diff changeset
85 if (T->isParameterPack())
anatofuz
parents:
diff changeset
86 addUnexpanded(T);
anatofuz
parents:
diff changeset
87
anatofuz
parents:
diff changeset
88 return true;
anatofuz
parents:
diff changeset
89 }
anatofuz
parents:
diff changeset
90
236
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
91 bool
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
92 VisitSubstTemplateTypeParmPackTypeLoc(SubstTemplateTypeParmPackTypeLoc TL) {
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
93 Unexpanded.push_back({TL.getTypePtr(), TL.getNameLoc()});
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
94 return true;
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
95 }
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
96
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
97 bool VisitSubstTemplateTypeParmPackType(SubstTemplateTypeParmPackType *T) {
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
98 Unexpanded.push_back({T, SourceLocation()});
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
99 return true;
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
100 }
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
101
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
102 bool
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
103 VisitSubstNonTypeTemplateParmPackExpr(SubstNonTypeTemplateParmPackExpr *E) {
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
104 Unexpanded.push_back({E, E->getParameterPackLocation()});
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
105 return true;
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
106 }
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
107
150
anatofuz
parents:
diff changeset
108 /// Record occurrences of function and non-type template
anatofuz
parents:
diff changeset
109 /// parameter packs in an expression.
anatofuz
parents:
diff changeset
110 bool VisitDeclRefExpr(DeclRefExpr *E) {
anatofuz
parents:
diff changeset
111 if (E->getDecl()->isParameterPack())
anatofuz
parents:
diff changeset
112 addUnexpanded(E->getDecl(), E->getLocation());
anatofuz
parents:
diff changeset
113
anatofuz
parents:
diff changeset
114 return true;
anatofuz
parents:
diff changeset
115 }
anatofuz
parents:
diff changeset
116
anatofuz
parents:
diff changeset
117 /// Record occurrences of template template parameter packs.
anatofuz
parents:
diff changeset
118 bool TraverseTemplateName(TemplateName Template) {
anatofuz
parents:
diff changeset
119 if (auto *TTP = dyn_cast_or_null<TemplateTemplateParmDecl>(
anatofuz
parents:
diff changeset
120 Template.getAsTemplateDecl())) {
anatofuz
parents:
diff changeset
121 if (TTP->isParameterPack())
anatofuz
parents:
diff changeset
122 addUnexpanded(TTP);
anatofuz
parents:
diff changeset
123 }
anatofuz
parents:
diff changeset
124
anatofuz
parents:
diff changeset
125 return inherited::TraverseTemplateName(Template);
anatofuz
parents:
diff changeset
126 }
anatofuz
parents:
diff changeset
127
anatofuz
parents:
diff changeset
128 /// Suppress traversal into Objective-C container literal
anatofuz
parents:
diff changeset
129 /// elements that are pack expansions.
anatofuz
parents:
diff changeset
130 bool TraverseObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
anatofuz
parents:
diff changeset
131 if (!E->containsUnexpandedParameterPack())
anatofuz
parents:
diff changeset
132 return true;
anatofuz
parents:
diff changeset
133
anatofuz
parents:
diff changeset
134 for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
anatofuz
parents:
diff changeset
135 ObjCDictionaryElement Element = E->getKeyValueElement(I);
anatofuz
parents:
diff changeset
136 if (Element.isPackExpansion())
anatofuz
parents:
diff changeset
137 continue;
anatofuz
parents:
diff changeset
138
anatofuz
parents:
diff changeset
139 TraverseStmt(Element.Key);
anatofuz
parents:
diff changeset
140 TraverseStmt(Element.Value);
anatofuz
parents:
diff changeset
141 }
anatofuz
parents:
diff changeset
142 return true;
anatofuz
parents:
diff changeset
143 }
anatofuz
parents:
diff changeset
144 //------------------------------------------------------------------------
anatofuz
parents:
diff changeset
145 // Pruning the search for unexpanded parameter packs.
anatofuz
parents:
diff changeset
146 //------------------------------------------------------------------------
anatofuz
parents:
diff changeset
147
anatofuz
parents:
diff changeset
148 /// Suppress traversal into statements and expressions that
anatofuz
parents:
diff changeset
149 /// do not contain unexpanded parameter packs.
anatofuz
parents:
diff changeset
150 bool TraverseStmt(Stmt *S) {
anatofuz
parents:
diff changeset
151 Expr *E = dyn_cast_or_null<Expr>(S);
anatofuz
parents:
diff changeset
152 if ((E && E->containsUnexpandedParameterPack()) || InLambda)
anatofuz
parents:
diff changeset
153 return inherited::TraverseStmt(S);
anatofuz
parents:
diff changeset
154
anatofuz
parents:
diff changeset
155 return true;
anatofuz
parents:
diff changeset
156 }
anatofuz
parents:
diff changeset
157
anatofuz
parents:
diff changeset
158 /// Suppress traversal into types that do not contain
anatofuz
parents:
diff changeset
159 /// unexpanded parameter packs.
anatofuz
parents:
diff changeset
160 bool TraverseType(QualType T) {
anatofuz
parents:
diff changeset
161 if ((!T.isNull() && T->containsUnexpandedParameterPack()) || InLambda)
anatofuz
parents:
diff changeset
162 return inherited::TraverseType(T);
anatofuz
parents:
diff changeset
163
anatofuz
parents:
diff changeset
164 return true;
anatofuz
parents:
diff changeset
165 }
anatofuz
parents:
diff changeset
166
anatofuz
parents:
diff changeset
167 /// Suppress traversal into types with location information
anatofuz
parents:
diff changeset
168 /// that do not contain unexpanded parameter packs.
anatofuz
parents:
diff changeset
169 bool TraverseTypeLoc(TypeLoc TL) {
anatofuz
parents:
diff changeset
170 if ((!TL.getType().isNull() &&
anatofuz
parents:
diff changeset
171 TL.getType()->containsUnexpandedParameterPack()) ||
anatofuz
parents:
diff changeset
172 InLambda)
anatofuz
parents:
diff changeset
173 return inherited::TraverseTypeLoc(TL);
anatofuz
parents:
diff changeset
174
anatofuz
parents:
diff changeset
175 return true;
anatofuz
parents:
diff changeset
176 }
anatofuz
parents:
diff changeset
177
anatofuz
parents:
diff changeset
178 /// Suppress traversal of parameter packs.
anatofuz
parents:
diff changeset
179 bool TraverseDecl(Decl *D) {
anatofuz
parents:
diff changeset
180 // A function parameter pack is a pack expansion, so cannot contain
anatofuz
parents:
diff changeset
181 // an unexpanded parameter pack. Likewise for a template parameter
anatofuz
parents:
diff changeset
182 // pack that contains any references to other packs.
anatofuz
parents:
diff changeset
183 if (D && D->isParameterPack())
anatofuz
parents:
diff changeset
184 return true;
anatofuz
parents:
diff changeset
185
anatofuz
parents:
diff changeset
186 return inherited::TraverseDecl(D);
anatofuz
parents:
diff changeset
187 }
anatofuz
parents:
diff changeset
188
anatofuz
parents:
diff changeset
189 /// Suppress traversal of pack-expanded attributes.
anatofuz
parents:
diff changeset
190 bool TraverseAttr(Attr *A) {
anatofuz
parents:
diff changeset
191 if (A->isPackExpansion())
anatofuz
parents:
diff changeset
192 return true;
anatofuz
parents:
diff changeset
193
anatofuz
parents:
diff changeset
194 return inherited::TraverseAttr(A);
anatofuz
parents:
diff changeset
195 }
anatofuz
parents:
diff changeset
196
anatofuz
parents:
diff changeset
197 /// Suppress traversal of pack expansion expressions and types.
anatofuz
parents:
diff changeset
198 ///@{
anatofuz
parents:
diff changeset
199 bool TraversePackExpansionType(PackExpansionType *T) { return true; }
anatofuz
parents:
diff changeset
200 bool TraversePackExpansionTypeLoc(PackExpansionTypeLoc TL) { return true; }
anatofuz
parents:
diff changeset
201 bool TraversePackExpansionExpr(PackExpansionExpr *E) { return true; }
anatofuz
parents:
diff changeset
202 bool TraverseCXXFoldExpr(CXXFoldExpr *E) { return true; }
anatofuz
parents:
diff changeset
203
anatofuz
parents:
diff changeset
204 ///@}
anatofuz
parents:
diff changeset
205
anatofuz
parents:
diff changeset
206 /// Suppress traversal of using-declaration pack expansion.
anatofuz
parents:
diff changeset
207 bool TraverseUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
anatofuz
parents:
diff changeset
208 if (D->isPackExpansion())
anatofuz
parents:
diff changeset
209 return true;
anatofuz
parents:
diff changeset
210
anatofuz
parents:
diff changeset
211 return inherited::TraverseUnresolvedUsingValueDecl(D);
anatofuz
parents:
diff changeset
212 }
anatofuz
parents:
diff changeset
213
anatofuz
parents:
diff changeset
214 /// Suppress traversal of using-declaration pack expansion.
anatofuz
parents:
diff changeset
215 bool TraverseUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
anatofuz
parents:
diff changeset
216 if (D->isPackExpansion())
anatofuz
parents:
diff changeset
217 return true;
anatofuz
parents:
diff changeset
218
anatofuz
parents:
diff changeset
219 return inherited::TraverseUnresolvedUsingTypenameDecl(D);
anatofuz
parents:
diff changeset
220 }
anatofuz
parents:
diff changeset
221
anatofuz
parents:
diff changeset
222 /// Suppress traversal of template argument pack expansions.
anatofuz
parents:
diff changeset
223 bool TraverseTemplateArgument(const TemplateArgument &Arg) {
anatofuz
parents:
diff changeset
224 if (Arg.isPackExpansion())
anatofuz
parents:
diff changeset
225 return true;
anatofuz
parents:
diff changeset
226
anatofuz
parents:
diff changeset
227 return inherited::TraverseTemplateArgument(Arg);
anatofuz
parents:
diff changeset
228 }
anatofuz
parents:
diff changeset
229
anatofuz
parents:
diff changeset
230 /// Suppress traversal of template argument pack expansions.
anatofuz
parents:
diff changeset
231 bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) {
anatofuz
parents:
diff changeset
232 if (ArgLoc.getArgument().isPackExpansion())
anatofuz
parents:
diff changeset
233 return true;
anatofuz
parents:
diff changeset
234
anatofuz
parents:
diff changeset
235 return inherited::TraverseTemplateArgumentLoc(ArgLoc);
anatofuz
parents:
diff changeset
236 }
anatofuz
parents:
diff changeset
237
anatofuz
parents:
diff changeset
238 /// Suppress traversal of base specifier pack expansions.
anatofuz
parents:
diff changeset
239 bool TraverseCXXBaseSpecifier(const CXXBaseSpecifier &Base) {
anatofuz
parents:
diff changeset
240 if (Base.isPackExpansion())
anatofuz
parents:
diff changeset
241 return true;
anatofuz
parents:
diff changeset
242
anatofuz
parents:
diff changeset
243 return inherited::TraverseCXXBaseSpecifier(Base);
anatofuz
parents:
diff changeset
244 }
anatofuz
parents:
diff changeset
245
anatofuz
parents:
diff changeset
246 /// Suppress traversal of mem-initializer pack expansions.
anatofuz
parents:
diff changeset
247 bool TraverseConstructorInitializer(CXXCtorInitializer *Init) {
anatofuz
parents:
diff changeset
248 if (Init->isPackExpansion())
anatofuz
parents:
diff changeset
249 return true;
anatofuz
parents:
diff changeset
250
anatofuz
parents:
diff changeset
251 return inherited::TraverseConstructorInitializer(Init);
anatofuz
parents:
diff changeset
252 }
anatofuz
parents:
diff changeset
253
anatofuz
parents:
diff changeset
254 /// Note whether we're traversing a lambda containing an unexpanded
anatofuz
parents:
diff changeset
255 /// parameter pack. In this case, the unexpanded pack can occur anywhere,
anatofuz
parents:
diff changeset
256 /// including all the places where we normally wouldn't look. Within a
anatofuz
parents:
diff changeset
257 /// lambda, we don't propagate the 'contains unexpanded parameter pack' bit
anatofuz
parents:
diff changeset
258 /// outside an expression.
anatofuz
parents:
diff changeset
259 bool TraverseLambdaExpr(LambdaExpr *Lambda) {
anatofuz
parents:
diff changeset
260 // The ContainsUnexpandedParameterPack bit on a lambda is always correct,
anatofuz
parents:
diff changeset
261 // even if it's contained within another lambda.
anatofuz
parents:
diff changeset
262 if (!Lambda->containsUnexpandedParameterPack())
anatofuz
parents:
diff changeset
263 return true;
anatofuz
parents:
diff changeset
264
anatofuz
parents:
diff changeset
265 bool WasInLambda = InLambda;
anatofuz
parents:
diff changeset
266 unsigned OldDepthLimit = DepthLimit;
anatofuz
parents:
diff changeset
267
anatofuz
parents:
diff changeset
268 InLambda = true;
anatofuz
parents:
diff changeset
269 if (auto *TPL = Lambda->getTemplateParameterList())
anatofuz
parents:
diff changeset
270 DepthLimit = TPL->getDepth();
anatofuz
parents:
diff changeset
271
anatofuz
parents:
diff changeset
272 inherited::TraverseLambdaExpr(Lambda);
anatofuz
parents:
diff changeset
273
anatofuz
parents:
diff changeset
274 InLambda = WasInLambda;
anatofuz
parents:
diff changeset
275 DepthLimit = OldDepthLimit;
anatofuz
parents:
diff changeset
276 return true;
anatofuz
parents:
diff changeset
277 }
anatofuz
parents:
diff changeset
278
anatofuz
parents:
diff changeset
279 /// Suppress traversal within pack expansions in lambda captures.
anatofuz
parents:
diff changeset
280 bool TraverseLambdaCapture(LambdaExpr *Lambda, const LambdaCapture *C,
anatofuz
parents:
diff changeset
281 Expr *Init) {
anatofuz
parents:
diff changeset
282 if (C->isPackExpansion())
anatofuz
parents:
diff changeset
283 return true;
anatofuz
parents:
diff changeset
284
anatofuz
parents:
diff changeset
285 return inherited::TraverseLambdaCapture(Lambda, C, Init);
anatofuz
parents:
diff changeset
286 }
anatofuz
parents:
diff changeset
287 };
anatofuz
parents:
diff changeset
288 }
anatofuz
parents:
diff changeset
289
anatofuz
parents:
diff changeset
290 /// Determine whether it's possible for an unexpanded parameter pack to
anatofuz
parents:
diff changeset
291 /// be valid in this location. This only happens when we're in a declaration
anatofuz
parents:
diff changeset
292 /// that is nested within an expression that could be expanded, such as a
anatofuz
parents:
diff changeset
293 /// lambda-expression within a function call.
anatofuz
parents:
diff changeset
294 ///
anatofuz
parents:
diff changeset
295 /// This is conservatively correct, but may claim that some unexpanded packs are
anatofuz
parents:
diff changeset
296 /// permitted when they are not.
anatofuz
parents:
diff changeset
297 bool Sema::isUnexpandedParameterPackPermitted() {
anatofuz
parents:
diff changeset
298 for (auto *SI : FunctionScopes)
anatofuz
parents:
diff changeset
299 if (isa<sema::LambdaScopeInfo>(SI))
anatofuz
parents:
diff changeset
300 return true;
anatofuz
parents:
diff changeset
301 return false;
anatofuz
parents:
diff changeset
302 }
anatofuz
parents:
diff changeset
303
anatofuz
parents:
diff changeset
304 /// Diagnose all of the unexpanded parameter packs in the given
anatofuz
parents:
diff changeset
305 /// vector.
anatofuz
parents:
diff changeset
306 bool
anatofuz
parents:
diff changeset
307 Sema::DiagnoseUnexpandedParameterPacks(SourceLocation Loc,
anatofuz
parents:
diff changeset
308 UnexpandedParameterPackContext UPPC,
anatofuz
parents:
diff changeset
309 ArrayRef<UnexpandedParameterPack> Unexpanded) {
anatofuz
parents:
diff changeset
310 if (Unexpanded.empty())
anatofuz
parents:
diff changeset
311 return false;
anatofuz
parents:
diff changeset
312
anatofuz
parents:
diff changeset
313 // If we are within a lambda expression and referencing a pack that is not
anatofuz
parents:
diff changeset
314 // declared within the lambda itself, that lambda contains an unexpanded
anatofuz
parents:
diff changeset
315 // parameter pack, and we are done.
anatofuz
parents:
diff changeset
316 // FIXME: Store 'Unexpanded' on the lambda so we don't need to recompute it
anatofuz
parents:
diff changeset
317 // later.
anatofuz
parents:
diff changeset
318 SmallVector<UnexpandedParameterPack, 4> LambdaParamPackReferences;
anatofuz
parents:
diff changeset
319 if (auto *LSI = getEnclosingLambda()) {
anatofuz
parents:
diff changeset
320 for (auto &Pack : Unexpanded) {
anatofuz
parents:
diff changeset
321 auto DeclaresThisPack = [&](NamedDecl *LocalPack) {
anatofuz
parents:
diff changeset
322 if (auto *TTPT = Pack.first.dyn_cast<const TemplateTypeParmType *>()) {
anatofuz
parents:
diff changeset
323 auto *TTPD = dyn_cast<TemplateTypeParmDecl>(LocalPack);
anatofuz
parents:
diff changeset
324 return TTPD && TTPD->getTypeForDecl() == TTPT;
anatofuz
parents:
diff changeset
325 }
236
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
326 return declaresSameEntity(Pack.first.get<const NamedDecl *>(),
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
327 LocalPack);
150
anatofuz
parents:
diff changeset
328 };
236
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
329 if (llvm::any_of(LSI->LocalPacks, DeclaresThisPack))
150
anatofuz
parents:
diff changeset
330 LambdaParamPackReferences.push_back(Pack);
anatofuz
parents:
diff changeset
331 }
anatofuz
parents:
diff changeset
332
anatofuz
parents:
diff changeset
333 if (LambdaParamPackReferences.empty()) {
anatofuz
parents:
diff changeset
334 // Construct in lambda only references packs declared outside the lambda.
anatofuz
parents:
diff changeset
335 // That's OK for now, but the lambda itself is considered to contain an
anatofuz
parents:
diff changeset
336 // unexpanded pack in this case, which will require expansion outside the
anatofuz
parents:
diff changeset
337 // lambda.
anatofuz
parents:
diff changeset
338
anatofuz
parents:
diff changeset
339 // We do not permit pack expansion that would duplicate a statement
anatofuz
parents:
diff changeset
340 // expression, not even within a lambda.
anatofuz
parents:
diff changeset
341 // FIXME: We could probably support this for statement expressions that
anatofuz
parents:
diff changeset
342 // do not contain labels.
anatofuz
parents:
diff changeset
343 // FIXME: This is insufficient to detect this problem; consider
anatofuz
parents:
diff changeset
344 // f( ({ bad: 0; }) + pack ... );
anatofuz
parents:
diff changeset
345 bool EnclosingStmtExpr = false;
anatofuz
parents:
diff changeset
346 for (unsigned N = FunctionScopes.size(); N; --N) {
anatofuz
parents:
diff changeset
347 sema::FunctionScopeInfo *Func = FunctionScopes[N-1];
236
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
348 if (llvm::any_of(
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
349 Func->CompoundScopes,
150
anatofuz
parents:
diff changeset
350 [](sema::CompoundScopeInfo &CSI) { return CSI.IsStmtExpr; })) {
anatofuz
parents:
diff changeset
351 EnclosingStmtExpr = true;
anatofuz
parents:
diff changeset
352 break;
anatofuz
parents:
diff changeset
353 }
anatofuz
parents:
diff changeset
354 // Coumpound-statements outside the lambda are OK for now; we'll check
anatofuz
parents:
diff changeset
355 // for those when we finish handling the lambda.
anatofuz
parents:
diff changeset
356 if (Func == LSI)
anatofuz
parents:
diff changeset
357 break;
anatofuz
parents:
diff changeset
358 }
anatofuz
parents:
diff changeset
359
anatofuz
parents:
diff changeset
360 if (!EnclosingStmtExpr) {
anatofuz
parents:
diff changeset
361 LSI->ContainsUnexpandedParameterPack = true;
anatofuz
parents:
diff changeset
362 return false;
anatofuz
parents:
diff changeset
363 }
anatofuz
parents:
diff changeset
364 } else {
anatofuz
parents:
diff changeset
365 Unexpanded = LambdaParamPackReferences;
anatofuz
parents:
diff changeset
366 }
anatofuz
parents:
diff changeset
367 }
anatofuz
parents:
diff changeset
368
anatofuz
parents:
diff changeset
369 SmallVector<SourceLocation, 4> Locations;
anatofuz
parents:
diff changeset
370 SmallVector<IdentifierInfo *, 4> Names;
anatofuz
parents:
diff changeset
371 llvm::SmallPtrSet<IdentifierInfo *, 4> NamesKnown;
anatofuz
parents:
diff changeset
372
anatofuz
parents:
diff changeset
373 for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
anatofuz
parents:
diff changeset
374 IdentifierInfo *Name = nullptr;
anatofuz
parents:
diff changeset
375 if (const TemplateTypeParmType *TTP
anatofuz
parents:
diff changeset
376 = Unexpanded[I].first.dyn_cast<const TemplateTypeParmType *>())
anatofuz
parents:
diff changeset
377 Name = TTP->getIdentifier();
anatofuz
parents:
diff changeset
378 else
236
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
379 Name = Unexpanded[I].first.get<const NamedDecl *>()->getIdentifier();
150
anatofuz
parents:
diff changeset
380
anatofuz
parents:
diff changeset
381 if (Name && NamesKnown.insert(Name).second)
anatofuz
parents:
diff changeset
382 Names.push_back(Name);
anatofuz
parents:
diff changeset
383
anatofuz
parents:
diff changeset
384 if (Unexpanded[I].second.isValid())
anatofuz
parents:
diff changeset
385 Locations.push_back(Unexpanded[I].second);
anatofuz
parents:
diff changeset
386 }
anatofuz
parents:
diff changeset
387
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
388 auto DB = Diag(Loc, diag::err_unexpanded_parameter_pack)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
389 << (int)UPPC << (int)Names.size();
150
anatofuz
parents:
diff changeset
390 for (size_t I = 0, E = std::min(Names.size(), (size_t)2); I != E; ++I)
anatofuz
parents:
diff changeset
391 DB << Names[I];
anatofuz
parents:
diff changeset
392
anatofuz
parents:
diff changeset
393 for (unsigned I = 0, N = Locations.size(); I != N; ++I)
anatofuz
parents:
diff changeset
394 DB << SourceRange(Locations[I]);
anatofuz
parents:
diff changeset
395 return true;
anatofuz
parents:
diff changeset
396 }
anatofuz
parents:
diff changeset
397
anatofuz
parents:
diff changeset
398 bool Sema::DiagnoseUnexpandedParameterPack(SourceLocation Loc,
anatofuz
parents:
diff changeset
399 TypeSourceInfo *T,
anatofuz
parents:
diff changeset
400 UnexpandedParameterPackContext UPPC) {
anatofuz
parents:
diff changeset
401 // C++0x [temp.variadic]p5:
anatofuz
parents:
diff changeset
402 // An appearance of a name of a parameter pack that is not expanded is
anatofuz
parents:
diff changeset
403 // ill-formed.
anatofuz
parents:
diff changeset
404 if (!T->getType()->containsUnexpandedParameterPack())
anatofuz
parents:
diff changeset
405 return false;
anatofuz
parents:
diff changeset
406
anatofuz
parents:
diff changeset
407 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
anatofuz
parents:
diff changeset
408 CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseTypeLoc(
anatofuz
parents:
diff changeset
409 T->getTypeLoc());
anatofuz
parents:
diff changeset
410 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
anatofuz
parents:
diff changeset
411 return DiagnoseUnexpandedParameterPacks(Loc, UPPC, Unexpanded);
anatofuz
parents:
diff changeset
412 }
anatofuz
parents:
diff changeset
413
anatofuz
parents:
diff changeset
414 bool Sema::DiagnoseUnexpandedParameterPack(Expr *E,
anatofuz
parents:
diff changeset
415 UnexpandedParameterPackContext UPPC) {
anatofuz
parents:
diff changeset
416 // C++0x [temp.variadic]p5:
anatofuz
parents:
diff changeset
417 // An appearance of a name of a parameter pack that is not expanded is
anatofuz
parents:
diff changeset
418 // ill-formed.
anatofuz
parents:
diff changeset
419 if (!E->containsUnexpandedParameterPack())
anatofuz
parents:
diff changeset
420 return false;
anatofuz
parents:
diff changeset
421
anatofuz
parents:
diff changeset
422 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
anatofuz
parents:
diff changeset
423 CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseStmt(E);
anatofuz
parents:
diff changeset
424 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
anatofuz
parents:
diff changeset
425 return DiagnoseUnexpandedParameterPacks(E->getBeginLoc(), UPPC, Unexpanded);
anatofuz
parents:
diff changeset
426 }
anatofuz
parents:
diff changeset
427
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
428 bool Sema::DiagnoseUnexpandedParameterPackInRequiresExpr(RequiresExpr *RE) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
429 if (!RE->containsUnexpandedParameterPack())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
430 return false;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
431
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
432 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
433 CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseStmt(RE);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
434 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
435
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
436 // We only care about unexpanded references to the RequiresExpr's own
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
437 // parameter packs.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
438 auto Parms = RE->getLocalParameters();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
439 llvm::SmallPtrSet<NamedDecl*, 8> ParmSet(Parms.begin(), Parms.end());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
440 SmallVector<UnexpandedParameterPack, 2> UnexpandedParms;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
441 for (auto Parm : Unexpanded)
236
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
442 if (ParmSet.contains(Parm.first.dyn_cast<const NamedDecl *>()))
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
443 UnexpandedParms.push_back(Parm);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
444 if (UnexpandedParms.empty())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
445 return false;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
446
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
447 return DiagnoseUnexpandedParameterPacks(RE->getBeginLoc(), UPPC_Requirement,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
448 UnexpandedParms);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
449 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
450
150
anatofuz
parents:
diff changeset
451 bool Sema::DiagnoseUnexpandedParameterPack(const CXXScopeSpec &SS,
anatofuz
parents:
diff changeset
452 UnexpandedParameterPackContext UPPC) {
anatofuz
parents:
diff changeset
453 // C++0x [temp.variadic]p5:
anatofuz
parents:
diff changeset
454 // An appearance of a name of a parameter pack that is not expanded is
anatofuz
parents:
diff changeset
455 // ill-formed.
anatofuz
parents:
diff changeset
456 if (!SS.getScopeRep() ||
anatofuz
parents:
diff changeset
457 !SS.getScopeRep()->containsUnexpandedParameterPack())
anatofuz
parents:
diff changeset
458 return false;
anatofuz
parents:
diff changeset
459
anatofuz
parents:
diff changeset
460 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
anatofuz
parents:
diff changeset
461 CollectUnexpandedParameterPacksVisitor(Unexpanded)
anatofuz
parents:
diff changeset
462 .TraverseNestedNameSpecifier(SS.getScopeRep());
anatofuz
parents:
diff changeset
463 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
anatofuz
parents:
diff changeset
464 return DiagnoseUnexpandedParameterPacks(SS.getRange().getBegin(),
anatofuz
parents:
diff changeset
465 UPPC, Unexpanded);
anatofuz
parents:
diff changeset
466 }
anatofuz
parents:
diff changeset
467
anatofuz
parents:
diff changeset
468 bool Sema::DiagnoseUnexpandedParameterPack(const DeclarationNameInfo &NameInfo,
anatofuz
parents:
diff changeset
469 UnexpandedParameterPackContext UPPC) {
anatofuz
parents:
diff changeset
470 // C++0x [temp.variadic]p5:
anatofuz
parents:
diff changeset
471 // An appearance of a name of a parameter pack that is not expanded is
anatofuz
parents:
diff changeset
472 // ill-formed.
anatofuz
parents:
diff changeset
473 switch (NameInfo.getName().getNameKind()) {
anatofuz
parents:
diff changeset
474 case DeclarationName::Identifier:
anatofuz
parents:
diff changeset
475 case DeclarationName::ObjCZeroArgSelector:
anatofuz
parents:
diff changeset
476 case DeclarationName::ObjCOneArgSelector:
anatofuz
parents:
diff changeset
477 case DeclarationName::ObjCMultiArgSelector:
anatofuz
parents:
diff changeset
478 case DeclarationName::CXXOperatorName:
anatofuz
parents:
diff changeset
479 case DeclarationName::CXXLiteralOperatorName:
anatofuz
parents:
diff changeset
480 case DeclarationName::CXXUsingDirective:
anatofuz
parents:
diff changeset
481 case DeclarationName::CXXDeductionGuideName:
anatofuz
parents:
diff changeset
482 return false;
anatofuz
parents:
diff changeset
483
anatofuz
parents:
diff changeset
484 case DeclarationName::CXXConstructorName:
anatofuz
parents:
diff changeset
485 case DeclarationName::CXXDestructorName:
anatofuz
parents:
diff changeset
486 case DeclarationName::CXXConversionFunctionName:
anatofuz
parents:
diff changeset
487 // FIXME: We shouldn't need this null check!
anatofuz
parents:
diff changeset
488 if (TypeSourceInfo *TSInfo = NameInfo.getNamedTypeInfo())
anatofuz
parents:
diff changeset
489 return DiagnoseUnexpandedParameterPack(NameInfo.getLoc(), TSInfo, UPPC);
anatofuz
parents:
diff changeset
490
anatofuz
parents:
diff changeset
491 if (!NameInfo.getName().getCXXNameType()->containsUnexpandedParameterPack())
anatofuz
parents:
diff changeset
492 return false;
anatofuz
parents:
diff changeset
493
anatofuz
parents:
diff changeset
494 break;
anatofuz
parents:
diff changeset
495 }
anatofuz
parents:
diff changeset
496
anatofuz
parents:
diff changeset
497 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
anatofuz
parents:
diff changeset
498 CollectUnexpandedParameterPacksVisitor(Unexpanded)
anatofuz
parents:
diff changeset
499 .TraverseType(NameInfo.getName().getCXXNameType());
anatofuz
parents:
diff changeset
500 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
anatofuz
parents:
diff changeset
501 return DiagnoseUnexpandedParameterPacks(NameInfo.getLoc(), UPPC, Unexpanded);
anatofuz
parents:
diff changeset
502 }
anatofuz
parents:
diff changeset
503
anatofuz
parents:
diff changeset
504 bool Sema::DiagnoseUnexpandedParameterPack(SourceLocation Loc,
anatofuz
parents:
diff changeset
505 TemplateName Template,
anatofuz
parents:
diff changeset
506 UnexpandedParameterPackContext UPPC) {
anatofuz
parents:
diff changeset
507
anatofuz
parents:
diff changeset
508 if (Template.isNull() || !Template.containsUnexpandedParameterPack())
anatofuz
parents:
diff changeset
509 return false;
anatofuz
parents:
diff changeset
510
anatofuz
parents:
diff changeset
511 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
anatofuz
parents:
diff changeset
512 CollectUnexpandedParameterPacksVisitor(Unexpanded)
anatofuz
parents:
diff changeset
513 .TraverseTemplateName(Template);
anatofuz
parents:
diff changeset
514 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
anatofuz
parents:
diff changeset
515 return DiagnoseUnexpandedParameterPacks(Loc, UPPC, Unexpanded);
anatofuz
parents:
diff changeset
516 }
anatofuz
parents:
diff changeset
517
anatofuz
parents:
diff changeset
518 bool Sema::DiagnoseUnexpandedParameterPack(TemplateArgumentLoc Arg,
anatofuz
parents:
diff changeset
519 UnexpandedParameterPackContext UPPC) {
anatofuz
parents:
diff changeset
520 if (Arg.getArgument().isNull() ||
anatofuz
parents:
diff changeset
521 !Arg.getArgument().containsUnexpandedParameterPack())
anatofuz
parents:
diff changeset
522 return false;
anatofuz
parents:
diff changeset
523
anatofuz
parents:
diff changeset
524 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
anatofuz
parents:
diff changeset
525 CollectUnexpandedParameterPacksVisitor(Unexpanded)
anatofuz
parents:
diff changeset
526 .TraverseTemplateArgumentLoc(Arg);
anatofuz
parents:
diff changeset
527 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
anatofuz
parents:
diff changeset
528 return DiagnoseUnexpandedParameterPacks(Arg.getLocation(), UPPC, Unexpanded);
anatofuz
parents:
diff changeset
529 }
anatofuz
parents:
diff changeset
530
anatofuz
parents:
diff changeset
531 void Sema::collectUnexpandedParameterPacks(TemplateArgument Arg,
anatofuz
parents:
diff changeset
532 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
anatofuz
parents:
diff changeset
533 CollectUnexpandedParameterPacksVisitor(Unexpanded)
anatofuz
parents:
diff changeset
534 .TraverseTemplateArgument(Arg);
anatofuz
parents:
diff changeset
535 }
anatofuz
parents:
diff changeset
536
anatofuz
parents:
diff changeset
537 void Sema::collectUnexpandedParameterPacks(TemplateArgumentLoc Arg,
anatofuz
parents:
diff changeset
538 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
anatofuz
parents:
diff changeset
539 CollectUnexpandedParameterPacksVisitor(Unexpanded)
anatofuz
parents:
diff changeset
540 .TraverseTemplateArgumentLoc(Arg);
anatofuz
parents:
diff changeset
541 }
anatofuz
parents:
diff changeset
542
anatofuz
parents:
diff changeset
543 void Sema::collectUnexpandedParameterPacks(QualType T,
anatofuz
parents:
diff changeset
544 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
anatofuz
parents:
diff changeset
545 CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseType(T);
anatofuz
parents:
diff changeset
546 }
anatofuz
parents:
diff changeset
547
anatofuz
parents:
diff changeset
548 void Sema::collectUnexpandedParameterPacks(TypeLoc TL,
anatofuz
parents:
diff changeset
549 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
anatofuz
parents:
diff changeset
550 CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseTypeLoc(TL);
anatofuz
parents:
diff changeset
551 }
anatofuz
parents:
diff changeset
552
anatofuz
parents:
diff changeset
553 void Sema::collectUnexpandedParameterPacks(
anatofuz
parents:
diff changeset
554 NestedNameSpecifierLoc NNS,
anatofuz
parents:
diff changeset
555 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
anatofuz
parents:
diff changeset
556 CollectUnexpandedParameterPacksVisitor(Unexpanded)
anatofuz
parents:
diff changeset
557 .TraverseNestedNameSpecifierLoc(NNS);
anatofuz
parents:
diff changeset
558 }
anatofuz
parents:
diff changeset
559
anatofuz
parents:
diff changeset
560 void Sema::collectUnexpandedParameterPacks(
anatofuz
parents:
diff changeset
561 const DeclarationNameInfo &NameInfo,
anatofuz
parents:
diff changeset
562 SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
anatofuz
parents:
diff changeset
563 CollectUnexpandedParameterPacksVisitor(Unexpanded)
anatofuz
parents:
diff changeset
564 .TraverseDeclarationNameInfo(NameInfo);
anatofuz
parents:
diff changeset
565 }
anatofuz
parents:
diff changeset
566
anatofuz
parents:
diff changeset
567
anatofuz
parents:
diff changeset
568 ParsedTemplateArgument
anatofuz
parents:
diff changeset
569 Sema::ActOnPackExpansion(const ParsedTemplateArgument &Arg,
anatofuz
parents:
diff changeset
570 SourceLocation EllipsisLoc) {
anatofuz
parents:
diff changeset
571 if (Arg.isInvalid())
anatofuz
parents:
diff changeset
572 return Arg;
anatofuz
parents:
diff changeset
573
anatofuz
parents:
diff changeset
574 switch (Arg.getKind()) {
anatofuz
parents:
diff changeset
575 case ParsedTemplateArgument::Type: {
anatofuz
parents:
diff changeset
576 TypeResult Result = ActOnPackExpansion(Arg.getAsType(), EllipsisLoc);
anatofuz
parents:
diff changeset
577 if (Result.isInvalid())
anatofuz
parents:
diff changeset
578 return ParsedTemplateArgument();
anatofuz
parents:
diff changeset
579
anatofuz
parents:
diff changeset
580 return ParsedTemplateArgument(Arg.getKind(), Result.get().getAsOpaquePtr(),
anatofuz
parents:
diff changeset
581 Arg.getLocation());
anatofuz
parents:
diff changeset
582 }
anatofuz
parents:
diff changeset
583
anatofuz
parents:
diff changeset
584 case ParsedTemplateArgument::NonType: {
anatofuz
parents:
diff changeset
585 ExprResult Result = ActOnPackExpansion(Arg.getAsExpr(), EllipsisLoc);
anatofuz
parents:
diff changeset
586 if (Result.isInvalid())
anatofuz
parents:
diff changeset
587 return ParsedTemplateArgument();
anatofuz
parents:
diff changeset
588
anatofuz
parents:
diff changeset
589 return ParsedTemplateArgument(Arg.getKind(), Result.get(),
anatofuz
parents:
diff changeset
590 Arg.getLocation());
anatofuz
parents:
diff changeset
591 }
anatofuz
parents:
diff changeset
592
anatofuz
parents:
diff changeset
593 case ParsedTemplateArgument::Template:
anatofuz
parents:
diff changeset
594 if (!Arg.getAsTemplate().get().containsUnexpandedParameterPack()) {
anatofuz
parents:
diff changeset
595 SourceRange R(Arg.getLocation());
anatofuz
parents:
diff changeset
596 if (Arg.getScopeSpec().isValid())
anatofuz
parents:
diff changeset
597 R.setBegin(Arg.getScopeSpec().getBeginLoc());
anatofuz
parents:
diff changeset
598 Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs)
anatofuz
parents:
diff changeset
599 << R;
anatofuz
parents:
diff changeset
600 return ParsedTemplateArgument();
anatofuz
parents:
diff changeset
601 }
anatofuz
parents:
diff changeset
602
anatofuz
parents:
diff changeset
603 return Arg.getTemplatePackExpansion(EllipsisLoc);
anatofuz
parents:
diff changeset
604 }
anatofuz
parents:
diff changeset
605 llvm_unreachable("Unhandled template argument kind?");
anatofuz
parents:
diff changeset
606 }
anatofuz
parents:
diff changeset
607
anatofuz
parents:
diff changeset
608 TypeResult Sema::ActOnPackExpansion(ParsedType Type,
anatofuz
parents:
diff changeset
609 SourceLocation EllipsisLoc) {
anatofuz
parents:
diff changeset
610 TypeSourceInfo *TSInfo;
anatofuz
parents:
diff changeset
611 GetTypeFromParser(Type, &TSInfo);
anatofuz
parents:
diff changeset
612 if (!TSInfo)
anatofuz
parents:
diff changeset
613 return true;
anatofuz
parents:
diff changeset
614
anatofuz
parents:
diff changeset
615 TypeSourceInfo *TSResult = CheckPackExpansion(TSInfo, EllipsisLoc, None);
anatofuz
parents:
diff changeset
616 if (!TSResult)
anatofuz
parents:
diff changeset
617 return true;
anatofuz
parents:
diff changeset
618
anatofuz
parents:
diff changeset
619 return CreateParsedType(TSResult->getType(), TSResult);
anatofuz
parents:
diff changeset
620 }
anatofuz
parents:
diff changeset
621
anatofuz
parents:
diff changeset
622 TypeSourceInfo *
anatofuz
parents:
diff changeset
623 Sema::CheckPackExpansion(TypeSourceInfo *Pattern, SourceLocation EllipsisLoc,
anatofuz
parents:
diff changeset
624 Optional<unsigned> NumExpansions) {
anatofuz
parents:
diff changeset
625 // Create the pack expansion type and source-location information.
anatofuz
parents:
diff changeset
626 QualType Result = CheckPackExpansion(Pattern->getType(),
anatofuz
parents:
diff changeset
627 Pattern->getTypeLoc().getSourceRange(),
anatofuz
parents:
diff changeset
628 EllipsisLoc, NumExpansions);
anatofuz
parents:
diff changeset
629 if (Result.isNull())
anatofuz
parents:
diff changeset
630 return nullptr;
anatofuz
parents:
diff changeset
631
anatofuz
parents:
diff changeset
632 TypeLocBuilder TLB;
anatofuz
parents:
diff changeset
633 TLB.pushFullCopy(Pattern->getTypeLoc());
anatofuz
parents:
diff changeset
634 PackExpansionTypeLoc TL = TLB.push<PackExpansionTypeLoc>(Result);
anatofuz
parents:
diff changeset
635 TL.setEllipsisLoc(EllipsisLoc);
anatofuz
parents:
diff changeset
636
anatofuz
parents:
diff changeset
637 return TLB.getTypeSourceInfo(Context, Result);
anatofuz
parents:
diff changeset
638 }
anatofuz
parents:
diff changeset
639
anatofuz
parents:
diff changeset
640 QualType Sema::CheckPackExpansion(QualType Pattern, SourceRange PatternRange,
anatofuz
parents:
diff changeset
641 SourceLocation EllipsisLoc,
anatofuz
parents:
diff changeset
642 Optional<unsigned> NumExpansions) {
anatofuz
parents:
diff changeset
643 // C++11 [temp.variadic]p5:
anatofuz
parents:
diff changeset
644 // The pattern of a pack expansion shall name one or more
anatofuz
parents:
diff changeset
645 // parameter packs that are not expanded by a nested pack
anatofuz
parents:
diff changeset
646 // expansion.
anatofuz
parents:
diff changeset
647 //
anatofuz
parents:
diff changeset
648 // A pattern containing a deduced type can't occur "naturally" but arises in
anatofuz
parents:
diff changeset
649 // the desugaring of an init-capture pack.
anatofuz
parents:
diff changeset
650 if (!Pattern->containsUnexpandedParameterPack() &&
anatofuz
parents:
diff changeset
651 !Pattern->getContainedDeducedType()) {
anatofuz
parents:
diff changeset
652 Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs)
anatofuz
parents:
diff changeset
653 << PatternRange;
anatofuz
parents:
diff changeset
654 return QualType();
anatofuz
parents:
diff changeset
655 }
anatofuz
parents:
diff changeset
656
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
657 return Context.getPackExpansionType(Pattern, NumExpansions,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
658 /*ExpectPackInType=*/false);
150
anatofuz
parents:
diff changeset
659 }
anatofuz
parents:
diff changeset
660
anatofuz
parents:
diff changeset
661 ExprResult Sema::ActOnPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc) {
anatofuz
parents:
diff changeset
662 return CheckPackExpansion(Pattern, EllipsisLoc, None);
anatofuz
parents:
diff changeset
663 }
anatofuz
parents:
diff changeset
664
anatofuz
parents:
diff changeset
665 ExprResult Sema::CheckPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc,
anatofuz
parents:
diff changeset
666 Optional<unsigned> NumExpansions) {
anatofuz
parents:
diff changeset
667 if (!Pattern)
anatofuz
parents:
diff changeset
668 return ExprError();
anatofuz
parents:
diff changeset
669
anatofuz
parents:
diff changeset
670 // C++0x [temp.variadic]p5:
anatofuz
parents:
diff changeset
671 // The pattern of a pack expansion shall name one or more
anatofuz
parents:
diff changeset
672 // parameter packs that are not expanded by a nested pack
anatofuz
parents:
diff changeset
673 // expansion.
anatofuz
parents:
diff changeset
674 if (!Pattern->containsUnexpandedParameterPack()) {
anatofuz
parents:
diff changeset
675 Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs)
anatofuz
parents:
diff changeset
676 << Pattern->getSourceRange();
anatofuz
parents:
diff changeset
677 CorrectDelayedTyposInExpr(Pattern);
anatofuz
parents:
diff changeset
678 return ExprError();
anatofuz
parents:
diff changeset
679 }
anatofuz
parents:
diff changeset
680
anatofuz
parents:
diff changeset
681 // Create the pack expansion expression and source-location information.
anatofuz
parents:
diff changeset
682 return new (Context)
anatofuz
parents:
diff changeset
683 PackExpansionExpr(Context.DependentTy, Pattern, EllipsisLoc, NumExpansions);
anatofuz
parents:
diff changeset
684 }
anatofuz
parents:
diff changeset
685
anatofuz
parents:
diff changeset
686 bool Sema::CheckParameterPacksForExpansion(
anatofuz
parents:
diff changeset
687 SourceLocation EllipsisLoc, SourceRange PatternRange,
anatofuz
parents:
diff changeset
688 ArrayRef<UnexpandedParameterPack> Unexpanded,
anatofuz
parents:
diff changeset
689 const MultiLevelTemplateArgumentList &TemplateArgs, bool &ShouldExpand,
anatofuz
parents:
diff changeset
690 bool &RetainExpansion, Optional<unsigned> &NumExpansions) {
anatofuz
parents:
diff changeset
691 ShouldExpand = true;
anatofuz
parents:
diff changeset
692 RetainExpansion = false;
236
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
693 std::pair<const IdentifierInfo *, SourceLocation> FirstPack;
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
694 Optional<std::pair<unsigned, SourceLocation>> PartialExpansion;
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
695 Optional<unsigned> CurNumExpansions;
150
anatofuz
parents:
diff changeset
696
236
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
697 for (auto [P, Loc] : Unexpanded) {
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
698 // Compute the depth and index for this parameter pack.
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
699 Optional<std::pair<unsigned, unsigned>> Pos;
150
anatofuz
parents:
diff changeset
700 unsigned NewPackSize;
236
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
701 const auto *ND = P.dyn_cast<const NamedDecl *>();
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
702 if (ND && isa<VarDecl>(ND)) {
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
703 const auto *DAP =
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
704 CurrentInstantiationScope->findInstantiationOf(ND)
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
705 ->dyn_cast<LocalInstantiationScope::DeclArgumentPack *>();
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
706 if (!DAP) {
150
anatofuz
parents:
diff changeset
707 // We can't expand this function parameter pack, so we can't expand
anatofuz
parents:
diff changeset
708 // the pack expansion.
anatofuz
parents:
diff changeset
709 ShouldExpand = false;
anatofuz
parents:
diff changeset
710 continue;
anatofuz
parents:
diff changeset
711 }
236
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
712 NewPackSize = DAP->size();
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
713 } else if (ND) {
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
714 Pos = getDepthAndIndex(ND);
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
715 } else if (const auto *TTP = P.dyn_cast<const TemplateTypeParmType *>()) {
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
716 Pos = {TTP->getDepth(), TTP->getIndex()};
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
717 ND = TTP->getDecl();
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
718 // FIXME: We either should have some fallback for canonical TTP, or
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
719 // never have canonical TTP here.
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
720 } else if (const auto *STP =
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
721 P.dyn_cast<const SubstTemplateTypeParmPackType *>()) {
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
722 NewPackSize = STP->getNumArgs();
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
723 ND = STP->getReplacedParameter();
150
anatofuz
parents:
diff changeset
724 } else {
236
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
725 const auto *SEP = P.get<const SubstNonTypeTemplateParmPackExpr *>();
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
726 NewPackSize = SEP->getArgumentPack().pack_size();
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
727 ND = SEP->getParameterPack();
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
728 }
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
729
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
730 if (Pos) {
150
anatofuz
parents:
diff changeset
731 // If we don't have a template argument at this depth/index, then we
anatofuz
parents:
diff changeset
732 // cannot expand the pack expansion. Make a note of this, but we still
anatofuz
parents:
diff changeset
733 // want to check any parameter packs we *do* have arguments for.
236
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
734 if (Pos->first >= TemplateArgs.getNumLevels() ||
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
735 !TemplateArgs.hasTemplateArgument(Pos->first, Pos->second)) {
150
anatofuz
parents:
diff changeset
736 ShouldExpand = false;
anatofuz
parents:
diff changeset
737 continue;
anatofuz
parents:
diff changeset
738 }
anatofuz
parents:
diff changeset
739 // Determine the size of the argument pack.
236
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
740 NewPackSize = TemplateArgs(Pos->first, Pos->second).pack_size();
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
741 // C++0x [temp.arg.explicit]p9:
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
742 // Template argument deduction can extend the sequence of template
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
743 // arguments corresponding to a template parameter pack, even when the
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
744 // sequence contains explicitly specified template arguments.
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
745 if (CurrentInstantiationScope)
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
746 if (const NamedDecl *PartialPack =
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
747 CurrentInstantiationScope->getPartiallySubstitutedPack();
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
748 PartialPack && getDepthAndIndex(PartialPack) == *Pos) {
150
anatofuz
parents:
diff changeset
749 RetainExpansion = true;
anatofuz
parents:
diff changeset
750 // We don't actually know the new pack size yet.
236
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
751 PartialExpansion = {NewPackSize, Loc};
150
anatofuz
parents:
diff changeset
752 continue;
anatofuz
parents:
diff changeset
753 }
anatofuz
parents:
diff changeset
754 }
anatofuz
parents:
diff changeset
755
236
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
756 // FIXME: Workaround for Canonical TTP.
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
757 const IdentifierInfo *Name = ND ? ND->getIdentifier() : nullptr;
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
758 if (!CurNumExpansions) {
150
anatofuz
parents:
diff changeset
759 // The is the first pack we've seen for which we have an argument.
anatofuz
parents:
diff changeset
760 // Record it.
236
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
761 CurNumExpansions = NewPackSize;
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
762 FirstPack = {Name, Loc};
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
763 } else if (NewPackSize != *CurNumExpansions) {
150
anatofuz
parents:
diff changeset
764 // C++0x [temp.variadic]p5:
anatofuz
parents:
diff changeset
765 // All of the parameter packs expanded by a pack expansion shall have
anatofuz
parents:
diff changeset
766 // the same number of arguments specified.
236
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
767 Diag(EllipsisLoc, diag::err_pack_expansion_length_conflict)
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
768 << FirstPack.first << Name << *CurNumExpansions << NewPackSize
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
769 << SourceRange(FirstPack.second) << SourceRange(Loc);
150
anatofuz
parents:
diff changeset
770 return true;
anatofuz
parents:
diff changeset
771 }
anatofuz
parents:
diff changeset
772 }
anatofuz
parents:
diff changeset
773
236
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
774 if (NumExpansions && CurNumExpansions &&
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
775 *NumExpansions != *CurNumExpansions) {
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
776 Diag(EllipsisLoc, diag::err_pack_expansion_length_conflict_multilevel)
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
777 << FirstPack.first << *CurNumExpansions << *NumExpansions
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
778 << SourceRange(FirstPack.second);
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
779 return true;
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
780 }
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
781
150
anatofuz
parents:
diff changeset
782 // If we're performing a partial expansion but we also have a full expansion,
anatofuz
parents:
diff changeset
783 // expand to the number of common arguments. For example, given:
anatofuz
parents:
diff changeset
784 //
anatofuz
parents:
diff changeset
785 // template<typename ...T> struct A {
anatofuz
parents:
diff changeset
786 // template<typename ...U> void f(pair<T, U>...);
anatofuz
parents:
diff changeset
787 // };
anatofuz
parents:
diff changeset
788 //
anatofuz
parents:
diff changeset
789 // ... a call to 'A<int, int>().f<int>' should expand the pack once and
anatofuz
parents:
diff changeset
790 // retain an expansion.
236
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
791 if (PartialExpansion) {
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
792 if (CurNumExpansions && *CurNumExpansions < PartialExpansion->first) {
150
anatofuz
parents:
diff changeset
793 NamedDecl *PartialPack =
anatofuz
parents:
diff changeset
794 CurrentInstantiationScope->getPartiallySubstitutedPack();
anatofuz
parents:
diff changeset
795 Diag(EllipsisLoc, diag::err_pack_expansion_length_conflict_partial)
236
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
796 << PartialPack << PartialExpansion->first << *CurNumExpansions
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
797 << SourceRange(PartialExpansion->second);
150
anatofuz
parents:
diff changeset
798 return true;
anatofuz
parents:
diff changeset
799 }
236
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
800 NumExpansions = PartialExpansion->first;
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
801 } else {
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
802 NumExpansions = CurNumExpansions;
150
anatofuz
parents:
diff changeset
803 }
anatofuz
parents:
diff changeset
804
anatofuz
parents:
diff changeset
805 return false;
anatofuz
parents:
diff changeset
806 }
anatofuz
parents:
diff changeset
807
anatofuz
parents:
diff changeset
808 Optional<unsigned> Sema::getNumArgumentsInExpansion(QualType T,
anatofuz
parents:
diff changeset
809 const MultiLevelTemplateArgumentList &TemplateArgs) {
anatofuz
parents:
diff changeset
810 QualType Pattern = cast<PackExpansionType>(T)->getPattern();
anatofuz
parents:
diff changeset
811 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
anatofuz
parents:
diff changeset
812 CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseType(Pattern);
anatofuz
parents:
diff changeset
813
anatofuz
parents:
diff changeset
814 Optional<unsigned> Result;
236
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
815 auto setResultSz = [&Result](unsigned Size) {
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
816 assert((!Result || *Result == Size) && "inconsistent pack sizes");
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
817 Result = Size;
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
818 };
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
819 auto setResultPos = [&](const std::pair<unsigned, unsigned> &Pos) -> bool {
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
820 unsigned Depth = Pos.first, Index = Pos.second;
150
anatofuz
parents:
diff changeset
821 if (Depth >= TemplateArgs.getNumLevels() ||
anatofuz
parents:
diff changeset
822 !TemplateArgs.hasTemplateArgument(Depth, Index))
anatofuz
parents:
diff changeset
823 // The pattern refers to an unknown template argument. We're not ready to
anatofuz
parents:
diff changeset
824 // expand this pack yet.
236
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
825 return true;
150
anatofuz
parents:
diff changeset
826 // Determine the size of the argument pack.
236
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
827 setResultSz(TemplateArgs(Depth, Index).pack_size());
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
828 return false;
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
829 };
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
830
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
831 for (auto [I, _] : Unexpanded) {
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
832 if (const auto *TTP = I.dyn_cast<const TemplateTypeParmType *>()) {
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
833 if (setResultPos({TTP->getDepth(), TTP->getIndex()}))
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
834 return None;
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
835 } else if (const auto *STP =
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
836 I.dyn_cast<const SubstTemplateTypeParmPackType *>()) {
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
837 setResultSz(STP->getNumArgs());
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
838 } else if (const auto *SEP =
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
839 I.dyn_cast<const SubstNonTypeTemplateParmPackExpr *>()) {
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
840 setResultSz(SEP->getArgumentPack().pack_size());
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
841 } else {
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
842 const auto *ND = I.get<const NamedDecl *>();
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
843 // Function parameter pack or init-capture pack.
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
844 if (isa<VarDecl>(ND)) {
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
845 const auto *DAP =
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
846 CurrentInstantiationScope->findInstantiationOf(ND)
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
847 ->dyn_cast<LocalInstantiationScope::DeclArgumentPack *>();
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
848 if (!DAP)
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
849 // The pattern refers to an unexpanded pack. We're not ready to expand
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
850 // this pack yet.
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
851 return None;
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
852 setResultSz(DAP->size());
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
853 } else if (setResultPos(getDepthAndIndex(ND))) {
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
854 return None;
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
855 }
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
856 }
150
anatofuz
parents:
diff changeset
857 }
anatofuz
parents:
diff changeset
858
anatofuz
parents:
diff changeset
859 return Result;
anatofuz
parents:
diff changeset
860 }
anatofuz
parents:
diff changeset
861
anatofuz
parents:
diff changeset
862 bool Sema::containsUnexpandedParameterPacks(Declarator &D) {
anatofuz
parents:
diff changeset
863 const DeclSpec &DS = D.getDeclSpec();
anatofuz
parents:
diff changeset
864 switch (DS.getTypeSpecType()) {
anatofuz
parents:
diff changeset
865 case TST_typename:
236
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
866 case TST_typeof_unqualType:
150
anatofuz
parents:
diff changeset
867 case TST_typeofType:
236
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
868 #define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) case TST_##Trait:
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
869 #include "clang/Basic/TransformTypeTraits.def"
150
anatofuz
parents:
diff changeset
870 case TST_atomic: {
anatofuz
parents:
diff changeset
871 QualType T = DS.getRepAsType().get();
anatofuz
parents:
diff changeset
872 if (!T.isNull() && T->containsUnexpandedParameterPack())
anatofuz
parents:
diff changeset
873 return true;
anatofuz
parents:
diff changeset
874 break;
anatofuz
parents:
diff changeset
875 }
anatofuz
parents:
diff changeset
876
236
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
877 case TST_typeof_unqualExpr:
150
anatofuz
parents:
diff changeset
878 case TST_typeofExpr:
anatofuz
parents:
diff changeset
879 case TST_decltype:
236
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
880 case TST_bitint:
150
anatofuz
parents:
diff changeset
881 if (DS.getRepAsExpr() &&
anatofuz
parents:
diff changeset
882 DS.getRepAsExpr()->containsUnexpandedParameterPack())
anatofuz
parents:
diff changeset
883 return true;
anatofuz
parents:
diff changeset
884 break;
anatofuz
parents:
diff changeset
885
anatofuz
parents:
diff changeset
886 case TST_unspecified:
anatofuz
parents:
diff changeset
887 case TST_void:
anatofuz
parents:
diff changeset
888 case TST_char:
anatofuz
parents:
diff changeset
889 case TST_wchar:
anatofuz
parents:
diff changeset
890 case TST_char8:
anatofuz
parents:
diff changeset
891 case TST_char16:
anatofuz
parents:
diff changeset
892 case TST_char32:
anatofuz
parents:
diff changeset
893 case TST_int:
anatofuz
parents:
diff changeset
894 case TST_int128:
anatofuz
parents:
diff changeset
895 case TST_half:
anatofuz
parents:
diff changeset
896 case TST_float:
anatofuz
parents:
diff changeset
897 case TST_double:
anatofuz
parents:
diff changeset
898 case TST_Accum:
anatofuz
parents:
diff changeset
899 case TST_Fract:
anatofuz
parents:
diff changeset
900 case TST_Float16:
anatofuz
parents:
diff changeset
901 case TST_float128:
236
c4bab56944e8 LLVM 16
kono
parents: 207
diff changeset
902 case TST_ibm128:
150
anatofuz
parents:
diff changeset
903 case TST_bool:
anatofuz
parents:
diff changeset
904 case TST_decimal32:
anatofuz
parents:
diff changeset
905 case TST_decimal64:
anatofuz
parents:
diff changeset
906 case TST_decimal128:
anatofuz
parents:
diff changeset
907 case TST_enum:
anatofuz
parents:
diff changeset
908 case TST_union:
anatofuz
parents:
diff changeset
909 case TST_struct:
anatofuz
parents:
diff changeset
910 case TST_interface:
anatofuz
parents:
diff changeset
911 case TST_class:
anatofuz
parents:
diff changeset
912 case TST_auto:
anatofuz
parents:
diff changeset
913 case TST_auto_type:
anatofuz
parents:
diff changeset
914 case TST_decltype_auto:
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
915 case TST_BFloat16:
150
anatofuz
parents:
diff changeset
916 #define GENERIC_IMAGE_TYPE(ImgType, Id) case TST_##ImgType##_t:
anatofuz
parents:
diff changeset
917 #include "clang/Basic/OpenCLImageTypes.def"
anatofuz
parents:
diff changeset
918 case TST_unknown_anytype:
anatofuz
parents:
diff changeset
919 case TST_error:
anatofuz
parents:
diff changeset
920 break;
anatofuz
parents:
diff changeset
921 }
anatofuz
parents:
diff changeset
922
anatofuz
parents:
diff changeset
923 for (unsigned I = 0, N = D.getNumTypeObjects(); I != N; ++I) {
anatofuz
parents:
diff changeset
924 const DeclaratorChunk &Chunk = D.getTypeObject(I);
anatofuz
parents:
diff changeset
925 switch (Chunk.Kind) {
anatofuz
parents:
diff changeset
926 case DeclaratorChunk::Pointer:
anatofuz
parents:
diff changeset
927 case DeclaratorChunk::Reference:
anatofuz
parents:
diff changeset
928 case DeclaratorChunk::Paren:
anatofuz
parents:
diff changeset
929 case DeclaratorChunk::Pipe:
anatofuz
parents:
diff changeset
930 case DeclaratorChunk::BlockPointer:
anatofuz
parents:
diff changeset
931 // These declarator chunks cannot contain any parameter packs.
anatofuz
parents:
diff changeset
932 break;
anatofuz
parents:
diff changeset
933
anatofuz
parents:
diff changeset
934 case DeclaratorChunk::Array:
anatofuz
parents:
diff changeset
935 if (Chunk.Arr.NumElts &&
anatofuz
parents:
diff changeset
936 Chunk.Arr.NumElts->containsUnexpandedParameterPack())
anatofuz
parents:
diff changeset
937 return true;
anatofuz
parents:
diff changeset
938 break;
anatofuz
parents:
diff changeset
939 case DeclaratorChunk::Function:
anatofuz
parents:
diff changeset
940 for (unsigned i = 0, e = Chunk.Fun.NumParams; i != e; ++i) {
anatofuz
parents:
diff changeset
941 ParmVarDecl *Param = cast<ParmVarDecl>(Chunk.Fun.Params[i].Param);
anatofuz
parents:
diff changeset
942 QualType ParamTy = Param->getType();
anatofuz
parents:
diff changeset
943 assert(!ParamTy.isNull() && "Couldn't parse type?");
anatofuz
parents:
diff changeset
944 if (ParamTy->containsUnexpandedParameterPack()) return true;
anatofuz
parents:
diff changeset
945 }
anatofuz
parents:
diff changeset
946
anatofuz
parents:
diff changeset
947 if (Chunk.Fun.getExceptionSpecType() == EST_Dynamic) {
anatofuz
parents:
diff changeset
948 for (unsigned i = 0; i != Chunk.Fun.getNumExceptions(); ++i) {
anatofuz
parents:
diff changeset
949 if (Chunk.Fun.Exceptions[i]
anatofuz
parents:
diff changeset
950 .Ty.get()
anatofuz
parents:
diff changeset
951 ->containsUnexpandedParameterPack())
anatofuz
parents:
diff changeset
952 return true;
anatofuz
parents:
diff changeset
953 }
anatofuz
parents:
diff changeset
954 } else if (isComputedNoexcept(Chunk.Fun.getExceptionSpecType()) &&
anatofuz
parents:
diff changeset
955 Chunk.Fun.NoexceptExpr->containsUnexpandedParameterPack())
anatofuz
parents:
diff changeset
956 return true;
anatofuz
parents:
diff changeset
957
anatofuz
parents:
diff changeset
958 if (Chunk.Fun.hasTrailingReturnType()) {
anatofuz
parents:
diff changeset
959 QualType T = Chunk.Fun.getTrailingReturnType().get();
anatofuz
parents:
diff changeset
960 if (!T.isNull() && T->containsUnexpandedParameterPack())
anatofuz
parents:
diff changeset
961 return true;
anatofuz
parents:
diff changeset
962 }
anatofuz
parents:
diff changeset
963 break;
anatofuz
parents:
diff changeset
964
anatofuz
parents:
diff changeset
965 case DeclaratorChunk::MemberPointer:
anatofuz
parents:
diff changeset
966 if (Chunk.Mem.Scope().getScopeRep() &&
anatofuz
parents:
diff changeset
967 Chunk.Mem.Scope().getScopeRep()->containsUnexpandedParameterPack())
anatofuz
parents:
diff changeset
968 return true;
anatofuz
parents:
diff changeset
969 break;
anatofuz
parents:
diff changeset
970 }
anatofuz
parents:
diff changeset
971 }
anatofuz
parents:
diff changeset
972
anatofuz
parents:
diff changeset
973 if (Expr *TRC = D.getTrailingRequiresClause())
anatofuz
parents:
diff changeset
974 if (TRC->containsUnexpandedParameterPack())
anatofuz
parents:
diff changeset
975 return true;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
976
150
anatofuz
parents:
diff changeset
977 return false;
anatofuz
parents:
diff changeset
978 }
anatofuz
parents:
diff changeset
979
anatofuz
parents:
diff changeset
980 namespace {
anatofuz
parents:
diff changeset
981
anatofuz
parents:
diff changeset
982 // Callback to only accept typo corrections that refer to parameter packs.
anatofuz
parents:
diff changeset
983 class ParameterPackValidatorCCC final : public CorrectionCandidateCallback {
anatofuz
parents:
diff changeset
984 public:
anatofuz
parents:
diff changeset
985 bool ValidateCandidate(const TypoCorrection &candidate) override {
anatofuz
parents:
diff changeset
986 NamedDecl *ND = candidate.getCorrectionDecl();
anatofuz
parents:
diff changeset
987 return ND && ND->isParameterPack();
anatofuz
parents:
diff changeset
988 }
anatofuz
parents:
diff changeset
989
anatofuz
parents:
diff changeset
990 std::unique_ptr<CorrectionCandidateCallback> clone() override {
anatofuz
parents:
diff changeset
991 return std::make_unique<ParameterPackValidatorCCC>(*this);
anatofuz
parents:
diff changeset
992 }
anatofuz
parents:
diff changeset
993 };
anatofuz
parents:
diff changeset
994
anatofuz
parents:
diff changeset
995 }
anatofuz
parents:
diff changeset
996
anatofuz
parents:
diff changeset
997 /// Called when an expression computing the size of a parameter pack
anatofuz
parents:
diff changeset
998 /// is parsed.
anatofuz
parents:
diff changeset
999 ///
anatofuz
parents:
diff changeset
1000 /// \code
anatofuz
parents:
diff changeset
1001 /// template<typename ...Types> struct count {
anatofuz
parents:
diff changeset
1002 /// static const unsigned value = sizeof...(Types);
anatofuz
parents:
diff changeset
1003 /// };
anatofuz
parents:
diff changeset
1004 /// \endcode
anatofuz
parents:
diff changeset
1005 ///
anatofuz
parents:
diff changeset
1006 //
anatofuz
parents:
diff changeset
1007 /// \param OpLoc The location of the "sizeof" keyword.
anatofuz
parents:
diff changeset
1008 /// \param Name The name of the parameter pack whose size will be determined.
anatofuz
parents:
diff changeset
1009 /// \param NameLoc The source location of the name of the parameter pack.
anatofuz
parents:
diff changeset
1010 /// \param RParenLoc The location of the closing parentheses.
anatofuz
parents:
diff changeset
1011 ExprResult Sema::ActOnSizeofParameterPackExpr(Scope *S,
anatofuz
parents:
diff changeset
1012 SourceLocation OpLoc,
anatofuz
parents:
diff changeset
1013 IdentifierInfo &Name,
anatofuz
parents:
diff changeset
1014 SourceLocation NameLoc,
anatofuz
parents:
diff changeset
1015 SourceLocation RParenLoc) {
anatofuz
parents:
diff changeset
1016 // C++0x [expr.sizeof]p5:
anatofuz
parents:
diff changeset
1017 // The identifier in a sizeof... expression shall name a parameter pack.
anatofuz
parents:
diff changeset
1018 LookupResult R(*this, &Name, NameLoc, LookupOrdinaryName);
anatofuz
parents:
diff changeset
1019 LookupName(R, S);
anatofuz
parents:
diff changeset
1020
anatofuz
parents:
diff changeset
1021 NamedDecl *ParameterPack = nullptr;
anatofuz
parents:
diff changeset
1022 switch (R.getResultKind()) {
anatofuz
parents:
diff changeset
1023 case LookupResult::Found:
anatofuz
parents:
diff changeset
1024 ParameterPack = R.getFoundDecl();
anatofuz
parents:
diff changeset
1025 break;
anatofuz
parents:
diff changeset
1026
anatofuz
parents:
diff changeset
1027 case LookupResult::NotFound:
anatofuz
parents:
diff changeset
1028 case LookupResult::NotFoundInCurrentInstantiation: {
anatofuz
parents:
diff changeset
1029 ParameterPackValidatorCCC CCC{};
anatofuz
parents:
diff changeset
1030 if (TypoCorrection Corrected =
anatofuz
parents:
diff changeset
1031 CorrectTypo(R.getLookupNameInfo(), R.getLookupKind(), S, nullptr,
anatofuz
parents:
diff changeset
1032 CCC, CTK_ErrorRecovery)) {
anatofuz
parents:
diff changeset
1033 diagnoseTypo(Corrected,
anatofuz
parents:
diff changeset
1034 PDiag(diag::err_sizeof_pack_no_pack_name_suggest) << &Name,
anatofuz
parents:
diff changeset
1035 PDiag(diag::note_parameter_pack_here));
anatofuz
parents:
diff changeset
1036 ParameterPack = Corrected.getCorrectionDecl();
anatofuz
parents:
diff changeset
1037 }
anatofuz
parents:
diff changeset
1038 break;
anatofuz
parents:
diff changeset
1039 }
anatofuz
parents:
diff changeset
1040 case LookupResult::FoundOverloaded:
anatofuz
parents:
diff changeset
1041 case LookupResult::FoundUnresolvedValue:
anatofuz
parents:
diff changeset
1042 break;
anatofuz
parents:
diff changeset
1043
anatofuz
parents:
diff changeset
1044 case LookupResult::Ambiguous:
anatofuz
parents:
diff changeset
1045 DiagnoseAmbiguousLookup(R);
anatofuz
parents:
diff changeset
1046 return ExprError();
anatofuz
parents:
diff changeset
1047 }
anatofuz
parents:
diff changeset
1048
anatofuz
parents:
diff changeset
1049 if (!ParameterPack || !ParameterPack->isParameterPack()) {
anatofuz
parents:
diff changeset
1050 Diag(NameLoc, diag::err_sizeof_pack_no_pack_name)
anatofuz
parents:
diff changeset
1051 << &Name;
anatofuz
parents:
diff changeset
1052 return ExprError();
anatofuz
parents:
diff changeset
1053 }
anatofuz
parents:
diff changeset
1054
anatofuz
parents:
diff changeset
1055 MarkAnyDeclReferenced(OpLoc, ParameterPack, true);
anatofuz
parents:
diff changeset
1056
anatofuz
parents:
diff changeset
1057 return SizeOfPackExpr::Create(Context, OpLoc, ParameterPack, NameLoc,
anatofuz
parents:
diff changeset
1058 RParenLoc);
anatofuz
parents:
diff changeset
1059 }
anatofuz
parents:
diff changeset
1060
anatofuz
parents:
diff changeset
1061 TemplateArgumentLoc
anatofuz
parents:
diff changeset
1062 Sema::getTemplateArgumentPackExpansionPattern(
anatofuz
parents:
diff changeset
1063 TemplateArgumentLoc OrigLoc,
anatofuz
parents:
diff changeset
1064 SourceLocation &Ellipsis, Optional<unsigned> &NumExpansions) const {
anatofuz
parents:
diff changeset
1065 const TemplateArgument &Argument = OrigLoc.getArgument();
anatofuz
parents:
diff changeset
1066 assert(Argument.isPackExpansion());
anatofuz
parents:
diff changeset
1067 switch (Argument.getKind()) {
anatofuz
parents:
diff changeset
1068 case TemplateArgument::Type: {
anatofuz
parents:
diff changeset
1069 // FIXME: We shouldn't ever have to worry about missing
anatofuz
parents:
diff changeset
1070 // type-source info!
anatofuz
parents:
diff changeset
1071 TypeSourceInfo *ExpansionTSInfo = OrigLoc.getTypeSourceInfo();
anatofuz
parents:
diff changeset
1072 if (!ExpansionTSInfo)
anatofuz
parents:
diff changeset
1073 ExpansionTSInfo = Context.getTrivialTypeSourceInfo(Argument.getAsType(),
anatofuz
parents:
diff changeset
1074 Ellipsis);
anatofuz
parents:
diff changeset
1075 PackExpansionTypeLoc Expansion =
anatofuz
parents:
diff changeset
1076 ExpansionTSInfo->getTypeLoc().castAs<PackExpansionTypeLoc>();
anatofuz
parents:
diff changeset
1077 Ellipsis = Expansion.getEllipsisLoc();
anatofuz
parents:
diff changeset
1078
anatofuz
parents:
diff changeset
1079 TypeLoc Pattern = Expansion.getPatternLoc();
anatofuz
parents:
diff changeset
1080 NumExpansions = Expansion.getTypePtr()->getNumExpansions();
anatofuz
parents:
diff changeset
1081
anatofuz
parents:
diff changeset
1082 // We need to copy the TypeLoc because TemplateArgumentLocs store a
anatofuz
parents:
diff changeset
1083 // TypeSourceInfo.
anatofuz
parents:
diff changeset
1084 // FIXME: Find some way to avoid the copy?
anatofuz
parents:
diff changeset
1085 TypeLocBuilder TLB;
anatofuz
parents:
diff changeset
1086 TLB.pushFullCopy(Pattern);
anatofuz
parents:
diff changeset
1087 TypeSourceInfo *PatternTSInfo =
anatofuz
parents:
diff changeset
1088 TLB.getTypeSourceInfo(Context, Pattern.getType());
anatofuz
parents:
diff changeset
1089 return TemplateArgumentLoc(TemplateArgument(Pattern.getType()),
anatofuz
parents:
diff changeset
1090 PatternTSInfo);
anatofuz
parents:
diff changeset
1091 }
anatofuz
parents:
diff changeset
1092
anatofuz
parents:
diff changeset
1093 case TemplateArgument::Expression: {
anatofuz
parents:
diff changeset
1094 PackExpansionExpr *Expansion
anatofuz
parents:
diff changeset
1095 = cast<PackExpansionExpr>(Argument.getAsExpr());
anatofuz
parents:
diff changeset
1096 Expr *Pattern = Expansion->getPattern();
anatofuz
parents:
diff changeset
1097 Ellipsis = Expansion->getEllipsisLoc();
anatofuz
parents:
diff changeset
1098 NumExpansions = Expansion->getNumExpansions();
anatofuz
parents:
diff changeset
1099 return TemplateArgumentLoc(Pattern, Pattern);
anatofuz
parents:
diff changeset
1100 }
anatofuz
parents:
diff changeset
1101
anatofuz
parents:
diff changeset
1102 case TemplateArgument::TemplateExpansion:
anatofuz
parents:
diff changeset
1103 Ellipsis = OrigLoc.getTemplateEllipsisLoc();
anatofuz
parents:
diff changeset
1104 NumExpansions = Argument.getNumTemplateExpansions();
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1105 return TemplateArgumentLoc(Context, Argument.getPackExpansionPattern(),
150
anatofuz
parents:
diff changeset
1106 OrigLoc.getTemplateQualifierLoc(),
anatofuz
parents:
diff changeset
1107 OrigLoc.getTemplateNameLoc());
anatofuz
parents:
diff changeset
1108
anatofuz
parents:
diff changeset
1109 case TemplateArgument::Declaration:
anatofuz
parents:
diff changeset
1110 case TemplateArgument::NullPtr:
anatofuz
parents:
diff changeset
1111 case TemplateArgument::Template:
anatofuz
parents:
diff changeset
1112 case TemplateArgument::Integral:
anatofuz
parents:
diff changeset
1113 case TemplateArgument::Pack:
anatofuz
parents:
diff changeset
1114 case TemplateArgument::Null:
anatofuz
parents:
diff changeset
1115 return TemplateArgumentLoc();
anatofuz
parents:
diff changeset
1116 }
anatofuz
parents:
diff changeset
1117
anatofuz
parents:
diff changeset
1118 llvm_unreachable("Invalid TemplateArgument Kind!");
anatofuz
parents:
diff changeset
1119 }
anatofuz
parents:
diff changeset
1120
anatofuz
parents:
diff changeset
1121 Optional<unsigned> Sema::getFullyPackExpandedSize(TemplateArgument Arg) {
anatofuz
parents:
diff changeset
1122 assert(Arg.containsUnexpandedParameterPack());
anatofuz
parents:
diff changeset
1123
anatofuz
parents:
diff changeset
1124 // If this is a substituted pack, grab that pack. If not, we don't know
anatofuz
parents:
diff changeset
1125 // the size yet.
anatofuz
parents:
diff changeset
1126 // FIXME: We could find a size in more cases by looking for a substituted
anatofuz
parents:
diff changeset
1127 // pack anywhere within this argument, but that's not necessary in the common
anatofuz
parents:
diff changeset
1128 // case for 'sizeof...(A)' handling.
anatofuz
parents:
diff changeset
1129 TemplateArgument Pack;
anatofuz
parents:
diff changeset
1130 switch (Arg.getKind()) {
anatofuz
parents:
diff changeset
1131 case TemplateArgument::Type:
anatofuz
parents:
diff changeset
1132 if (auto *Subst = Arg.getAsType()->getAs<SubstTemplateTypeParmPackType>())
anatofuz
parents:
diff changeset
1133 Pack = Subst->getArgumentPack();
anatofuz
parents:
diff changeset
1134 else
anatofuz
parents:
diff changeset
1135 return None;
anatofuz
parents:
diff changeset
1136 break;
anatofuz
parents:
diff changeset
1137
anatofuz
parents:
diff changeset
1138 case TemplateArgument::Expression:
anatofuz
parents:
diff changeset
1139 if (auto *Subst =
anatofuz
parents:
diff changeset
1140 dyn_cast<SubstNonTypeTemplateParmPackExpr>(Arg.getAsExpr()))
anatofuz
parents:
diff changeset
1141 Pack = Subst->getArgumentPack();
anatofuz
parents:
diff changeset
1142 else if (auto *Subst = dyn_cast<FunctionParmPackExpr>(Arg.getAsExpr())) {
anatofuz
parents:
diff changeset
1143 for (VarDecl *PD : *Subst)
anatofuz
parents:
diff changeset
1144 if (PD->isParameterPack())
anatofuz
parents:
diff changeset
1145 return None;
anatofuz
parents:
diff changeset
1146 return Subst->getNumExpansions();
anatofuz
parents:
diff changeset
1147 } else
anatofuz
parents:
diff changeset
1148 return None;
anatofuz
parents:
diff changeset
1149 break;
anatofuz
parents:
diff changeset
1150
anatofuz
parents:
diff changeset
1151 case TemplateArgument::Template:
anatofuz
parents:
diff changeset
1152 if (SubstTemplateTemplateParmPackStorage *Subst =
anatofuz
parents:
diff changeset
1153 Arg.getAsTemplate().getAsSubstTemplateTemplateParmPack())
anatofuz
parents:
diff changeset
1154 Pack = Subst->getArgumentPack();
anatofuz
parents:
diff changeset
1155 else
anatofuz
parents:
diff changeset
1156 return None;
anatofuz
parents:
diff changeset
1157 break;
anatofuz
parents:
diff changeset
1158
anatofuz
parents:
diff changeset
1159 case TemplateArgument::Declaration:
anatofuz
parents:
diff changeset
1160 case TemplateArgument::NullPtr:
anatofuz
parents:
diff changeset
1161 case TemplateArgument::TemplateExpansion:
anatofuz
parents:
diff changeset
1162 case TemplateArgument::Integral:
anatofuz
parents:
diff changeset
1163 case TemplateArgument::Pack:
anatofuz
parents:
diff changeset
1164 case TemplateArgument::Null:
anatofuz
parents:
diff changeset
1165 return None;
anatofuz
parents:
diff changeset
1166 }
anatofuz
parents:
diff changeset
1167
anatofuz
parents:
diff changeset
1168 // Check that no argument in the pack is itself a pack expansion.
anatofuz
parents:
diff changeset
1169 for (TemplateArgument Elem : Pack.pack_elements()) {
anatofuz
parents:
diff changeset
1170 // There's no point recursing in this case; we would have already
anatofuz
parents:
diff changeset
1171 // expanded this pack expansion into the enclosing pack if we could.
anatofuz
parents:
diff changeset
1172 if (Elem.isPackExpansion())
anatofuz
parents:
diff changeset
1173 return None;
anatofuz
parents:
diff changeset
1174 }
anatofuz
parents:
diff changeset
1175 return Pack.pack_size();
anatofuz
parents:
diff changeset
1176 }
anatofuz
parents:
diff changeset
1177
anatofuz
parents:
diff changeset
1178 static void CheckFoldOperand(Sema &S, Expr *E) {
anatofuz
parents:
diff changeset
1179 if (!E)
anatofuz
parents:
diff changeset
1180 return;
anatofuz
parents:
diff changeset
1181
anatofuz
parents:
diff changeset
1182 E = E->IgnoreImpCasts();
anatofuz
parents:
diff changeset
1183 auto *OCE = dyn_cast<CXXOperatorCallExpr>(E);
anatofuz
parents:
diff changeset
1184 if ((OCE && OCE->isInfixBinaryOp()) || isa<BinaryOperator>(E) ||
anatofuz
parents:
diff changeset
1185 isa<AbstractConditionalOperator>(E)) {
anatofuz
parents:
diff changeset
1186 S.Diag(E->getExprLoc(), diag::err_fold_expression_bad_operand)
anatofuz
parents:
diff changeset
1187 << E->getSourceRange()
anatofuz
parents:
diff changeset
1188 << FixItHint::CreateInsertion(E->getBeginLoc(), "(")
anatofuz
parents:
diff changeset
1189 << FixItHint::CreateInsertion(E->getEndLoc(), ")");
anatofuz
parents:
diff changeset
1190 }
anatofuz
parents:
diff changeset
1191 }
anatofuz
parents:
diff changeset
1192
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1193 ExprResult Sema::ActOnCXXFoldExpr(Scope *S, SourceLocation LParenLoc, Expr *LHS,
150
anatofuz
parents:
diff changeset
1194 tok::TokenKind Operator,
anatofuz
parents:
diff changeset
1195 SourceLocation EllipsisLoc, Expr *RHS,
anatofuz
parents:
diff changeset
1196 SourceLocation RParenLoc) {
anatofuz
parents:
diff changeset
1197 // LHS and RHS must be cast-expressions. We allow an arbitrary expression
anatofuz
parents:
diff changeset
1198 // in the parser and reduce down to just cast-expressions here.
anatofuz
parents:
diff changeset
1199 CheckFoldOperand(*this, LHS);
anatofuz
parents:
diff changeset
1200 CheckFoldOperand(*this, RHS);
anatofuz
parents:
diff changeset
1201
anatofuz
parents:
diff changeset
1202 auto DiscardOperands = [&] {
anatofuz
parents:
diff changeset
1203 CorrectDelayedTyposInExpr(LHS);
anatofuz
parents:
diff changeset
1204 CorrectDelayedTyposInExpr(RHS);
anatofuz
parents:
diff changeset
1205 };
anatofuz
parents:
diff changeset
1206
anatofuz
parents:
diff changeset
1207 // [expr.prim.fold]p3:
anatofuz
parents:
diff changeset
1208 // In a binary fold, op1 and op2 shall be the same fold-operator, and
anatofuz
parents:
diff changeset
1209 // either e1 shall contain an unexpanded parameter pack or e2 shall contain
anatofuz
parents:
diff changeset
1210 // an unexpanded parameter pack, but not both.
anatofuz
parents:
diff changeset
1211 if (LHS && RHS &&
anatofuz
parents:
diff changeset
1212 LHS->containsUnexpandedParameterPack() ==
anatofuz
parents:
diff changeset
1213 RHS->containsUnexpandedParameterPack()) {
anatofuz
parents:
diff changeset
1214 DiscardOperands();
anatofuz
parents:
diff changeset
1215 return Diag(EllipsisLoc,
anatofuz
parents:
diff changeset
1216 LHS->containsUnexpandedParameterPack()
anatofuz
parents:
diff changeset
1217 ? diag::err_fold_expression_packs_both_sides
anatofuz
parents:
diff changeset
1218 : diag::err_pack_expansion_without_parameter_packs)
anatofuz
parents:
diff changeset
1219 << LHS->getSourceRange() << RHS->getSourceRange();
anatofuz
parents:
diff changeset
1220 }
anatofuz
parents:
diff changeset
1221
anatofuz
parents:
diff changeset
1222 // [expr.prim.fold]p2:
anatofuz
parents:
diff changeset
1223 // In a unary fold, the cast-expression shall contain an unexpanded
anatofuz
parents:
diff changeset
1224 // parameter pack.
anatofuz
parents:
diff changeset
1225 if (!LHS || !RHS) {
anatofuz
parents:
diff changeset
1226 Expr *Pack = LHS ? LHS : RHS;
anatofuz
parents:
diff changeset
1227 assert(Pack && "fold expression with neither LHS nor RHS");
anatofuz
parents:
diff changeset
1228 DiscardOperands();
anatofuz
parents:
diff changeset
1229 if (!Pack->containsUnexpandedParameterPack())
anatofuz
parents:
diff changeset
1230 return Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs)
anatofuz
parents:
diff changeset
1231 << Pack->getSourceRange();
anatofuz
parents:
diff changeset
1232 }
anatofuz
parents:
diff changeset
1233
anatofuz
parents:
diff changeset
1234 BinaryOperatorKind Opc = ConvertTokenKindToBinaryOpcode(Operator);
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1235
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1236 // Perform first-phase name lookup now.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1237 UnresolvedLookupExpr *ULE = nullptr;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1238 {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1239 UnresolvedSet<16> Functions;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1240 LookupBinOp(S, EllipsisLoc, Opc, Functions);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1241 if (!Functions.empty()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1242 DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1243 BinaryOperator::getOverloadedOperator(Opc));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1244 ExprResult Callee = CreateUnresolvedLookupExpr(
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1245 /*NamingClass*/ nullptr, NestedNameSpecifierLoc(),
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1246 DeclarationNameInfo(OpName, EllipsisLoc), Functions);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1247 if (Callee.isInvalid())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1248 return ExprError();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1249 ULE = cast<UnresolvedLookupExpr>(Callee.get());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1250 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1251 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1252
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1253 return BuildCXXFoldExpr(ULE, LParenLoc, LHS, Opc, EllipsisLoc, RHS, RParenLoc,
150
anatofuz
parents:
diff changeset
1254 None);
anatofuz
parents:
diff changeset
1255 }
anatofuz
parents:
diff changeset
1256
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1257 ExprResult Sema::BuildCXXFoldExpr(UnresolvedLookupExpr *Callee,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1258 SourceLocation LParenLoc, Expr *LHS,
150
anatofuz
parents:
diff changeset
1259 BinaryOperatorKind Operator,
anatofuz
parents:
diff changeset
1260 SourceLocation EllipsisLoc, Expr *RHS,
anatofuz
parents:
diff changeset
1261 SourceLocation RParenLoc,
anatofuz
parents:
diff changeset
1262 Optional<unsigned> NumExpansions) {
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1263 return new (Context)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1264 CXXFoldExpr(Context.DependentTy, Callee, LParenLoc, LHS, Operator,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
1265 EllipsisLoc, RHS, RParenLoc, NumExpansions);
150
anatofuz
parents:
diff changeset
1266 }
anatofuz
parents:
diff changeset
1267
anatofuz
parents:
diff changeset
1268 ExprResult Sema::BuildEmptyCXXFoldExpr(SourceLocation EllipsisLoc,
anatofuz
parents:
diff changeset
1269 BinaryOperatorKind Operator) {
anatofuz
parents:
diff changeset
1270 // [temp.variadic]p9:
anatofuz
parents:
diff changeset
1271 // If N is zero for a unary fold-expression, the value of the expression is
anatofuz
parents:
diff changeset
1272 // && -> true
anatofuz
parents:
diff changeset
1273 // || -> false
anatofuz
parents:
diff changeset
1274 // , -> void()
anatofuz
parents:
diff changeset
1275 // if the operator is not listed [above], the instantiation is ill-formed.
anatofuz
parents:
diff changeset
1276 //
anatofuz
parents:
diff changeset
1277 // Note that we need to use something like int() here, not merely 0, to
anatofuz
parents:
diff changeset
1278 // prevent the result from being a null pointer constant.
anatofuz
parents:
diff changeset
1279 QualType ScalarType;
anatofuz
parents:
diff changeset
1280 switch (Operator) {
anatofuz
parents:
diff changeset
1281 case BO_LOr:
anatofuz
parents:
diff changeset
1282 return ActOnCXXBoolLiteral(EllipsisLoc, tok::kw_false);
anatofuz
parents:
diff changeset
1283 case BO_LAnd:
anatofuz
parents:
diff changeset
1284 return ActOnCXXBoolLiteral(EllipsisLoc, tok::kw_true);
anatofuz
parents:
diff changeset
1285 case BO_Comma:
anatofuz
parents:
diff changeset
1286 ScalarType = Context.VoidTy;
anatofuz
parents:
diff changeset
1287 break;
anatofuz
parents:
diff changeset
1288
anatofuz
parents:
diff changeset
1289 default:
anatofuz
parents:
diff changeset
1290 return Diag(EllipsisLoc, diag::err_fold_expression_empty)
anatofuz
parents:
diff changeset
1291 << BinaryOperator::getOpcodeStr(Operator);
anatofuz
parents:
diff changeset
1292 }
anatofuz
parents:
diff changeset
1293
anatofuz
parents:
diff changeset
1294 return new (Context) CXXScalarValueInitExpr(
anatofuz
parents:
diff changeset
1295 ScalarType, Context.getTrivialTypeSourceInfo(ScalarType, EllipsisLoc),
anatofuz
parents:
diff changeset
1296 EllipsisLoc);
anatofuz
parents:
diff changeset
1297 }