annotate clang/lib/Sema/SemaTemplateVariadic.cpp @ 152:e8a9b4f4d755

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