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