1218887Sdim//===------- SemaTemplateVariadic.cpp - C++ Variadic Templates ------------===/
2218887Sdim//
3218887Sdim//                     The LLVM Compiler Infrastructure
4218887Sdim//
5218887Sdim// This file is distributed under the University of Illinois Open Source
6218887Sdim// License. See LICENSE.TXT for details.
7218887Sdim//===----------------------------------------------------------------------===/
8218887Sdim//
9218887Sdim//  This file implements semantic analysis for C++0x variadic templates.
10218887Sdim//===----------------------------------------------------------------------===/
11218887Sdim
12218887Sdim#include "clang/Sema/Sema.h"
13249423Sdim#include "clang/AST/Expr.h"
14249423Sdim#include "clang/AST/RecursiveASTVisitor.h"
15249423Sdim#include "clang/AST/TypeLoc.h"
16218887Sdim#include "clang/Sema/Lookup.h"
17218887Sdim#include "clang/Sema/ParsedTemplate.h"
18239462Sdim#include "clang/Sema/ScopeInfo.h"
19218887Sdim#include "clang/Sema/SemaInternal.h"
20218887Sdim#include "clang/Sema/Template.h"
21263508Sdim#include "TypeLocBuilder.h"
22218887Sdim
23218887Sdimusing namespace clang;
24218887Sdim
25218887Sdim//----------------------------------------------------------------------------
26218887Sdim// Visitor that collects unexpanded parameter packs
27218887Sdim//----------------------------------------------------------------------------
28218887Sdim
29218887Sdimnamespace {
30218887Sdim  /// \brief A class that collects unexpanded parameter packs.
31218887Sdim  class CollectUnexpandedParameterPacksVisitor :
32218887Sdim    public RecursiveASTVisitor<CollectUnexpandedParameterPacksVisitor>
33218887Sdim  {
34218887Sdim    typedef RecursiveASTVisitor<CollectUnexpandedParameterPacksVisitor>
35218887Sdim      inherited;
36218887Sdim
37226633Sdim    SmallVectorImpl<UnexpandedParameterPack> &Unexpanded;
38218887Sdim
39239462Sdim    bool InLambda;
40239462Sdim
41218887Sdim  public:
42218887Sdim    explicit CollectUnexpandedParameterPacksVisitor(
43226633Sdim                  SmallVectorImpl<UnexpandedParameterPack> &Unexpanded)
44239462Sdim      : Unexpanded(Unexpanded), InLambda(false) { }
45218887Sdim
46218887Sdim    bool shouldWalkTypesOfTypeLocs() const { return false; }
47218887Sdim
48218887Sdim    //------------------------------------------------------------------------
49218887Sdim    // Recording occurrences of (unexpanded) parameter packs.
50218887Sdim    //------------------------------------------------------------------------
51218887Sdim
52218887Sdim    /// \brief Record occurrences of template type parameter packs.
53218887Sdim    bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
54218887Sdim      if (TL.getTypePtr()->isParameterPack())
55218887Sdim        Unexpanded.push_back(std::make_pair(TL.getTypePtr(), TL.getNameLoc()));
56218887Sdim      return true;
57218887Sdim    }
58218887Sdim
59218887Sdim    /// \brief Record occurrences of template type parameter packs
60218887Sdim    /// when we don't have proper source-location information for
61218887Sdim    /// them.
62218887Sdim    ///
63218887Sdim    /// Ideally, this routine would never be used.
64218887Sdim    bool VisitTemplateTypeParmType(TemplateTypeParmType *T) {
65218887Sdim      if (T->isParameterPack())
66218887Sdim        Unexpanded.push_back(std::make_pair(T, SourceLocation()));
67218887Sdim
68218887Sdim      return true;
69218887Sdim    }
70218887Sdim
71218887Sdim    /// \brief Record occurrences of function and non-type template
72218887Sdim    /// parameter packs in an expression.
73218887Sdim    bool VisitDeclRefExpr(DeclRefExpr *E) {
74218887Sdim      if (E->getDecl()->isParameterPack())
75218887Sdim        Unexpanded.push_back(std::make_pair(E->getDecl(), E->getLocation()));
76218887Sdim
77218887Sdim      return true;
78218887Sdim    }
79218887Sdim
80218887Sdim    /// \brief Record occurrences of template template parameter packs.
81218887Sdim    bool TraverseTemplateName(TemplateName Template) {
82218887Sdim      if (TemplateTemplateParmDecl *TTP
83218887Sdim            = dyn_cast_or_null<TemplateTemplateParmDecl>(
84218887Sdim                                                  Template.getAsTemplateDecl()))
85218887Sdim        if (TTP->isParameterPack())
86218887Sdim          Unexpanded.push_back(std::make_pair(TTP, SourceLocation()));
87218887Sdim
88218887Sdim      return inherited::TraverseTemplateName(Template);
89218887Sdim    }
90218887Sdim
91234353Sdim    /// \brief Suppress traversal into Objective-C container literal
92234353Sdim    /// elements that are pack expansions.
93234353Sdim    bool TraverseObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
94234353Sdim      if (!E->containsUnexpandedParameterPack())
95234353Sdim        return true;
96234353Sdim
97234353Sdim      for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
98234353Sdim        ObjCDictionaryElement Element = E->getKeyValueElement(I);
99234353Sdim        if (Element.isPackExpansion())
100234353Sdim          continue;
101234353Sdim
102234353Sdim        TraverseStmt(Element.Key);
103234353Sdim        TraverseStmt(Element.Value);
104234353Sdim      }
105234353Sdim      return true;
106234353Sdim    }
107218887Sdim    //------------------------------------------------------------------------
108218887Sdim    // Pruning the search for unexpanded parameter packs.
109218887Sdim    //------------------------------------------------------------------------
110218887Sdim
111218887Sdim    /// \brief Suppress traversal into statements and expressions that
112218887Sdim    /// do not contain unexpanded parameter packs.
113218887Sdim    bool TraverseStmt(Stmt *S) {
114239462Sdim      Expr *E = dyn_cast_or_null<Expr>(S);
115239462Sdim      if ((E && E->containsUnexpandedParameterPack()) || InLambda)
116239462Sdim        return inherited::TraverseStmt(S);
117218887Sdim
118239462Sdim      return true;
119218887Sdim    }
120218887Sdim
121218887Sdim    /// \brief Suppress traversal into types that do not contain
122218887Sdim    /// unexpanded parameter packs.
123218887Sdim    bool TraverseType(QualType T) {
124239462Sdim      if ((!T.isNull() && T->containsUnexpandedParameterPack()) || InLambda)
125218887Sdim        return inherited::TraverseType(T);
126218887Sdim
127218887Sdim      return true;
128218887Sdim    }
129218887Sdim
130218887Sdim    /// \brief Suppress traversel into types with location information
131218887Sdim    /// that do not contain unexpanded parameter packs.
132218887Sdim    bool TraverseTypeLoc(TypeLoc TL) {
133239462Sdim      if ((!TL.getType().isNull() &&
134239462Sdim           TL.getType()->containsUnexpandedParameterPack()) ||
135239462Sdim          InLambda)
136218887Sdim        return inherited::TraverseTypeLoc(TL);
137218887Sdim
138218887Sdim      return true;
139218887Sdim    }
140218887Sdim
141218887Sdim    /// \brief Suppress traversal of non-parameter declarations, since
142218887Sdim    /// they cannot contain unexpanded parameter packs.
143218887Sdim    bool TraverseDecl(Decl *D) {
144239462Sdim      if ((D && isa<ParmVarDecl>(D)) || InLambda)
145218887Sdim        return inherited::TraverseDecl(D);
146218887Sdim
147239462Sdim      return true;
148218887Sdim    }
149218887Sdim
150218887Sdim    /// \brief Suppress traversal of template argument pack expansions.
151218887Sdim    bool TraverseTemplateArgument(const TemplateArgument &Arg) {
152218887Sdim      if (Arg.isPackExpansion())
153218887Sdim        return true;
154218887Sdim
155218887Sdim      return inherited::TraverseTemplateArgument(Arg);
156218887Sdim    }
157218887Sdim
158218887Sdim    /// \brief Suppress traversal of template argument pack expansions.
159218887Sdim    bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) {
160218887Sdim      if (ArgLoc.getArgument().isPackExpansion())
161218887Sdim        return true;
162218887Sdim
163218887Sdim      return inherited::TraverseTemplateArgumentLoc(ArgLoc);
164218887Sdim    }
165239462Sdim
166239462Sdim    /// \brief Note whether we're traversing a lambda containing an unexpanded
167239462Sdim    /// parameter pack. In this case, the unexpanded pack can occur anywhere,
168239462Sdim    /// including all the places where we normally wouldn't look. Within a
169239462Sdim    /// lambda, we don't propagate the 'contains unexpanded parameter pack' bit
170239462Sdim    /// outside an expression.
171239462Sdim    bool TraverseLambdaExpr(LambdaExpr *Lambda) {
172239462Sdim      // The ContainsUnexpandedParameterPack bit on a lambda is always correct,
173239462Sdim      // even if it's contained within another lambda.
174239462Sdim      if (!Lambda->containsUnexpandedParameterPack())
175239462Sdim        return true;
176239462Sdim
177239462Sdim      bool WasInLambda = InLambda;
178239462Sdim      InLambda = true;
179239462Sdim
180239462Sdim      // If any capture names a function parameter pack, that pack is expanded
181239462Sdim      // when the lambda is expanded.
182239462Sdim      for (LambdaExpr::capture_iterator I = Lambda->capture_begin(),
183263508Sdim                                        E = Lambda->capture_end();
184263508Sdim           I != E; ++I) {
185263508Sdim        if (I->capturesVariable()) {
186263508Sdim          VarDecl *VD = I->getCapturedVar();
187239462Sdim          if (VD->isParameterPack())
188239462Sdim            Unexpanded.push_back(std::make_pair(VD, I->getLocation()));
189263508Sdim        }
190263508Sdim      }
191239462Sdim
192239462Sdim      inherited::TraverseLambdaExpr(Lambda);
193239462Sdim
194239462Sdim      InLambda = WasInLambda;
195239462Sdim      return true;
196239462Sdim    }
197218887Sdim  };
198218887Sdim}
199218887Sdim
200218887Sdim/// \brief Diagnose all of the unexpanded parameter packs in the given
201218887Sdim/// vector.
202239462Sdimbool
203234353SdimSema::DiagnoseUnexpandedParameterPacks(SourceLocation Loc,
204234353Sdim                                       UnexpandedParameterPackContext UPPC,
205234353Sdim                                 ArrayRef<UnexpandedParameterPack> Unexpanded) {
206234353Sdim  if (Unexpanded.empty())
207239462Sdim    return false;
208239462Sdim
209239462Sdim  // If we are within a lambda expression, that lambda contains an unexpanded
210239462Sdim  // parameter pack, and we are done.
211239462Sdim  // FIXME: Store 'Unexpanded' on the lambda so we don't need to recompute it
212239462Sdim  // later.
213239462Sdim  for (unsigned N = FunctionScopes.size(); N; --N) {
214239462Sdim    if (sema::LambdaScopeInfo *LSI =
215239462Sdim          dyn_cast<sema::LambdaScopeInfo>(FunctionScopes[N-1])) {
216239462Sdim      LSI->ContainsUnexpandedParameterPack = true;
217239462Sdim      return false;
218239462Sdim    }
219239462Sdim  }
220234353Sdim
221226633Sdim  SmallVector<SourceLocation, 4> Locations;
222226633Sdim  SmallVector<IdentifierInfo *, 4> Names;
223218887Sdim  llvm::SmallPtrSet<IdentifierInfo *, 4> NamesKnown;
224218887Sdim
225218887Sdim  for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
226218887Sdim    IdentifierInfo *Name = 0;
227218887Sdim    if (const TemplateTypeParmType *TTP
228218887Sdim          = Unexpanded[I].first.dyn_cast<const TemplateTypeParmType *>())
229221345Sdim      Name = TTP->getIdentifier();
230218887Sdim    else
231218887Sdim      Name = Unexpanded[I].first.get<NamedDecl *>()->getIdentifier();
232218887Sdim
233218887Sdim    if (Name && NamesKnown.insert(Name))
234218887Sdim      Names.push_back(Name);
235218887Sdim
236218887Sdim    if (Unexpanded[I].second.isValid())
237218887Sdim      Locations.push_back(Unexpanded[I].second);
238218887Sdim  }
239218887Sdim
240218887Sdim  DiagnosticBuilder DB
241234353Sdim    = Names.size() == 0? Diag(Loc, diag::err_unexpanded_parameter_pack_0)
242218887Sdim                           << (int)UPPC
243234353Sdim    : Names.size() == 1? Diag(Loc, diag::err_unexpanded_parameter_pack_1)
244218887Sdim                           << (int)UPPC << Names[0]
245234353Sdim    : Names.size() == 2? Diag(Loc, diag::err_unexpanded_parameter_pack_2)
246218887Sdim                           << (int)UPPC << Names[0] << Names[1]
247234353Sdim    : Diag(Loc, diag::err_unexpanded_parameter_pack_3_or_more)
248218887Sdim        << (int)UPPC << Names[0] << Names[1];
249218887Sdim
250218887Sdim  for (unsigned I = 0, N = Locations.size(); I != N; ++I)
251218887Sdim    DB << SourceRange(Locations[I]);
252239462Sdim  return true;
253218887Sdim}
254218887Sdim
255218887Sdimbool Sema::DiagnoseUnexpandedParameterPack(SourceLocation Loc,
256218887Sdim                                           TypeSourceInfo *T,
257218887Sdim                                         UnexpandedParameterPackContext UPPC) {
258218887Sdim  // C++0x [temp.variadic]p5:
259218887Sdim  //   An appearance of a name of a parameter pack that is not expanded is
260218887Sdim  //   ill-formed.
261218887Sdim  if (!T->getType()->containsUnexpandedParameterPack())
262218887Sdim    return false;
263218887Sdim
264226633Sdim  SmallVector<UnexpandedParameterPack, 2> Unexpanded;
265218887Sdim  CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseTypeLoc(
266218887Sdim                                                              T->getTypeLoc());
267218887Sdim  assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
268239462Sdim  return DiagnoseUnexpandedParameterPacks(Loc, UPPC, Unexpanded);
269218887Sdim}
270218887Sdim
271218887Sdimbool Sema::DiagnoseUnexpandedParameterPack(Expr *E,
272218887Sdim                                        UnexpandedParameterPackContext UPPC) {
273218887Sdim  // C++0x [temp.variadic]p5:
274218887Sdim  //   An appearance of a name of a parameter pack that is not expanded is
275218887Sdim  //   ill-formed.
276218887Sdim  if (!E->containsUnexpandedParameterPack())
277218887Sdim    return false;
278218887Sdim
279226633Sdim  SmallVector<UnexpandedParameterPack, 2> Unexpanded;
280218887Sdim  CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseStmt(E);
281218887Sdim  assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
282239462Sdim  return DiagnoseUnexpandedParameterPacks(E->getLocStart(), UPPC, Unexpanded);
283218887Sdim}
284218887Sdim
285218887Sdimbool Sema::DiagnoseUnexpandedParameterPack(const CXXScopeSpec &SS,
286218887Sdim                                        UnexpandedParameterPackContext UPPC) {
287218887Sdim  // C++0x [temp.variadic]p5:
288218887Sdim  //   An appearance of a name of a parameter pack that is not expanded is
289218887Sdim  //   ill-formed.
290218887Sdim  if (!SS.getScopeRep() ||
291218887Sdim      !SS.getScopeRep()->containsUnexpandedParameterPack())
292218887Sdim    return false;
293218887Sdim
294226633Sdim  SmallVector<UnexpandedParameterPack, 2> Unexpanded;
295218887Sdim  CollectUnexpandedParameterPacksVisitor(Unexpanded)
296218887Sdim    .TraverseNestedNameSpecifier(SS.getScopeRep());
297218887Sdim  assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
298239462Sdim  return DiagnoseUnexpandedParameterPacks(SS.getRange().getBegin(),
299239462Sdim                                          UPPC, Unexpanded);
300218887Sdim}
301218887Sdim
302218887Sdimbool Sema::DiagnoseUnexpandedParameterPack(const DeclarationNameInfo &NameInfo,
303218887Sdim                                         UnexpandedParameterPackContext UPPC) {
304218887Sdim  // C++0x [temp.variadic]p5:
305218887Sdim  //   An appearance of a name of a parameter pack that is not expanded is
306218887Sdim  //   ill-formed.
307218887Sdim  switch (NameInfo.getName().getNameKind()) {
308218887Sdim  case DeclarationName::Identifier:
309218887Sdim  case DeclarationName::ObjCZeroArgSelector:
310218887Sdim  case DeclarationName::ObjCOneArgSelector:
311218887Sdim  case DeclarationName::ObjCMultiArgSelector:
312218887Sdim  case DeclarationName::CXXOperatorName:
313218887Sdim  case DeclarationName::CXXLiteralOperatorName:
314218887Sdim  case DeclarationName::CXXUsingDirective:
315218887Sdim    return false;
316218887Sdim
317218887Sdim  case DeclarationName::CXXConstructorName:
318218887Sdim  case DeclarationName::CXXDestructorName:
319218887Sdim  case DeclarationName::CXXConversionFunctionName:
320218887Sdim    // FIXME: We shouldn't need this null check!
321218887Sdim    if (TypeSourceInfo *TSInfo = NameInfo.getNamedTypeInfo())
322218887Sdim      return DiagnoseUnexpandedParameterPack(NameInfo.getLoc(), TSInfo, UPPC);
323218887Sdim
324218887Sdim    if (!NameInfo.getName().getCXXNameType()->containsUnexpandedParameterPack())
325218887Sdim      return false;
326218887Sdim
327218887Sdim    break;
328218887Sdim  }
329218887Sdim
330226633Sdim  SmallVector<UnexpandedParameterPack, 2> Unexpanded;
331218887Sdim  CollectUnexpandedParameterPacksVisitor(Unexpanded)
332218887Sdim    .TraverseType(NameInfo.getName().getCXXNameType());
333218887Sdim  assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
334239462Sdim  return DiagnoseUnexpandedParameterPacks(NameInfo.getLoc(), UPPC, Unexpanded);
335218887Sdim}
336218887Sdim
337218887Sdimbool Sema::DiagnoseUnexpandedParameterPack(SourceLocation Loc,
338218887Sdim                                           TemplateName Template,
339218887Sdim                                       UnexpandedParameterPackContext UPPC) {
340218887Sdim
341218887Sdim  if (Template.isNull() || !Template.containsUnexpandedParameterPack())
342218887Sdim    return false;
343218887Sdim
344226633Sdim  SmallVector<UnexpandedParameterPack, 2> Unexpanded;
345218887Sdim  CollectUnexpandedParameterPacksVisitor(Unexpanded)
346218887Sdim    .TraverseTemplateName(Template);
347218887Sdim  assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
348239462Sdim  return DiagnoseUnexpandedParameterPacks(Loc, UPPC, Unexpanded);
349218887Sdim}
350218887Sdim
351218887Sdimbool Sema::DiagnoseUnexpandedParameterPack(TemplateArgumentLoc Arg,
352218887Sdim                                         UnexpandedParameterPackContext UPPC) {
353218887Sdim  if (Arg.getArgument().isNull() ||
354218887Sdim      !Arg.getArgument().containsUnexpandedParameterPack())
355218887Sdim    return false;
356218887Sdim
357226633Sdim  SmallVector<UnexpandedParameterPack, 2> Unexpanded;
358218887Sdim  CollectUnexpandedParameterPacksVisitor(Unexpanded)
359218887Sdim    .TraverseTemplateArgumentLoc(Arg);
360218887Sdim  assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
361239462Sdim  return DiagnoseUnexpandedParameterPacks(Arg.getLocation(), UPPC, Unexpanded);
362218887Sdim}
363218887Sdim
364218887Sdimvoid Sema::collectUnexpandedParameterPacks(TemplateArgument Arg,
365226633Sdim                   SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
366218887Sdim  CollectUnexpandedParameterPacksVisitor(Unexpanded)
367218887Sdim    .TraverseTemplateArgument(Arg);
368218887Sdim}
369218887Sdim
370218887Sdimvoid Sema::collectUnexpandedParameterPacks(TemplateArgumentLoc Arg,
371226633Sdim                   SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
372218887Sdim  CollectUnexpandedParameterPacksVisitor(Unexpanded)
373218887Sdim    .TraverseTemplateArgumentLoc(Arg);
374218887Sdim}
375218887Sdim
376218887Sdimvoid Sema::collectUnexpandedParameterPacks(QualType T,
377226633Sdim                   SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
378218887Sdim  CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseType(T);
379218887Sdim}
380218887Sdim
381218887Sdimvoid Sema::collectUnexpandedParameterPacks(TypeLoc TL,
382226633Sdim                   SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
383218887Sdim  CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseTypeLoc(TL);
384218887Sdim}
385218887Sdim
386234353Sdimvoid Sema::collectUnexpandedParameterPacks(CXXScopeSpec &SS,
387234353Sdim                                           SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
388234353Sdim  NestedNameSpecifier *Qualifier = SS.getScopeRep();
389234353Sdim  if (!Qualifier)
390234353Sdim    return;
391234353Sdim
392234353Sdim  NestedNameSpecifierLoc QualifierLoc(Qualifier, SS.location_data());
393234353Sdim  CollectUnexpandedParameterPacksVisitor(Unexpanded)
394234353Sdim    .TraverseNestedNameSpecifierLoc(QualifierLoc);
395234353Sdim}
396234353Sdim
397234353Sdimvoid Sema::collectUnexpandedParameterPacks(const DeclarationNameInfo &NameInfo,
398234353Sdim                         SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
399234353Sdim  CollectUnexpandedParameterPacksVisitor(Unexpanded)
400234353Sdim    .TraverseDeclarationNameInfo(NameInfo);
401234353Sdim}
402234353Sdim
403234353Sdim
404218887SdimParsedTemplateArgument
405218887SdimSema::ActOnPackExpansion(const ParsedTemplateArgument &Arg,
406218887Sdim                         SourceLocation EllipsisLoc) {
407218887Sdim  if (Arg.isInvalid())
408218887Sdim    return Arg;
409218887Sdim
410218887Sdim  switch (Arg.getKind()) {
411218887Sdim  case ParsedTemplateArgument::Type: {
412218887Sdim    TypeResult Result = ActOnPackExpansion(Arg.getAsType(), EllipsisLoc);
413218887Sdim    if (Result.isInvalid())
414218887Sdim      return ParsedTemplateArgument();
415218887Sdim
416218887Sdim    return ParsedTemplateArgument(Arg.getKind(), Result.get().getAsOpaquePtr(),
417218887Sdim                                  Arg.getLocation());
418218887Sdim  }
419218887Sdim
420218887Sdim  case ParsedTemplateArgument::NonType: {
421218887Sdim    ExprResult Result = ActOnPackExpansion(Arg.getAsExpr(), EllipsisLoc);
422218887Sdim    if (Result.isInvalid())
423218887Sdim      return ParsedTemplateArgument();
424218887Sdim
425218887Sdim    return ParsedTemplateArgument(Arg.getKind(), Result.get(),
426218887Sdim                                  Arg.getLocation());
427218887Sdim  }
428218887Sdim
429218887Sdim  case ParsedTemplateArgument::Template:
430218887Sdim    if (!Arg.getAsTemplate().get().containsUnexpandedParameterPack()) {
431218887Sdim      SourceRange R(Arg.getLocation());
432218887Sdim      if (Arg.getScopeSpec().isValid())
433218887Sdim        R.setBegin(Arg.getScopeSpec().getBeginLoc());
434218887Sdim      Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs)
435218887Sdim        << R;
436218887Sdim      return ParsedTemplateArgument();
437218887Sdim    }
438218887Sdim
439218887Sdim    return Arg.getTemplatePackExpansion(EllipsisLoc);
440218887Sdim  }
441218887Sdim  llvm_unreachable("Unhandled template argument kind?");
442218887Sdim}
443218887Sdim
444218887SdimTypeResult Sema::ActOnPackExpansion(ParsedType Type,
445218887Sdim                                    SourceLocation EllipsisLoc) {
446218887Sdim  TypeSourceInfo *TSInfo;
447218887Sdim  GetTypeFromParser(Type, &TSInfo);
448218887Sdim  if (!TSInfo)
449218887Sdim    return true;
450218887Sdim
451249423Sdim  TypeSourceInfo *TSResult = CheckPackExpansion(TSInfo, EllipsisLoc, None);
452218887Sdim  if (!TSResult)
453218887Sdim    return true;
454218887Sdim
455218887Sdim  return CreateParsedType(TSResult->getType(), TSResult);
456218887Sdim}
457218887Sdim
458249423SdimTypeSourceInfo *
459249423SdimSema::CheckPackExpansion(TypeSourceInfo *Pattern, SourceLocation EllipsisLoc,
460249423Sdim                         Optional<unsigned> NumExpansions) {
461218887Sdim  // Create the pack expansion type and source-location information.
462218887Sdim  QualType Result = CheckPackExpansion(Pattern->getType(),
463218887Sdim                                       Pattern->getTypeLoc().getSourceRange(),
464218887Sdim                                       EllipsisLoc, NumExpansions);
465218887Sdim  if (Result.isNull())
466218887Sdim    return 0;
467263508Sdim
468263508Sdim  TypeLocBuilder TLB;
469263508Sdim  TLB.pushFullCopy(Pattern->getTypeLoc());
470263508Sdim  PackExpansionTypeLoc TL = TLB.push<PackExpansionTypeLoc>(Result);
471218887Sdim  TL.setEllipsisLoc(EllipsisLoc);
472263508Sdim
473263508Sdim  return TLB.getTypeSourceInfo(Context, Result);
474218887Sdim}
475218887Sdim
476249423SdimQualType Sema::CheckPackExpansion(QualType Pattern, SourceRange PatternRange,
477218887Sdim                                  SourceLocation EllipsisLoc,
478249423Sdim                                  Optional<unsigned> NumExpansions) {
479218887Sdim  // C++0x [temp.variadic]p5:
480218887Sdim  //   The pattern of a pack expansion shall name one or more
481218887Sdim  //   parameter packs that are not expanded by a nested pack
482218887Sdim  //   expansion.
483218887Sdim  if (!Pattern->containsUnexpandedParameterPack()) {
484218887Sdim    Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs)
485218887Sdim      << PatternRange;
486218887Sdim    return QualType();
487218887Sdim  }
488218887Sdim
489218887Sdim  return Context.getPackExpansionType(Pattern, NumExpansions);
490218887Sdim}
491218887Sdim
492218887SdimExprResult Sema::ActOnPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc) {
493249423Sdim  return CheckPackExpansion(Pattern, EllipsisLoc, None);
494218887Sdim}
495218887Sdim
496218887SdimExprResult Sema::CheckPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc,
497249423Sdim                                    Optional<unsigned> NumExpansions) {
498218887Sdim  if (!Pattern)
499218887Sdim    return ExprError();
500218887Sdim
501218887Sdim  // C++0x [temp.variadic]p5:
502218887Sdim  //   The pattern of a pack expansion shall name one or more
503218887Sdim  //   parameter packs that are not expanded by a nested pack
504218887Sdim  //   expansion.
505218887Sdim  if (!Pattern->containsUnexpandedParameterPack()) {
506218887Sdim    Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs)
507218887Sdim    << Pattern->getSourceRange();
508218887Sdim    return ExprError();
509218887Sdim  }
510218887Sdim
511218887Sdim  // Create the pack expansion expression and source-location information.
512218887Sdim  return Owned(new (Context) PackExpansionExpr(Context.DependentTy, Pattern,
513218887Sdim                                               EllipsisLoc, NumExpansions));
514218887Sdim}
515218887Sdim
516218887Sdim/// \brief Retrieve the depth and index of a parameter pack.
517218887Sdimstatic std::pair<unsigned, unsigned>
518218887SdimgetDepthAndIndex(NamedDecl *ND) {
519218887Sdim  if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND))
520218887Sdim    return std::make_pair(TTP->getDepth(), TTP->getIndex());
521218887Sdim
522218887Sdim  if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND))
523218887Sdim    return std::make_pair(NTTP->getDepth(), NTTP->getIndex());
524218887Sdim
525218887Sdim  TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND);
526218887Sdim  return std::make_pair(TTP->getDepth(), TTP->getIndex());
527218887Sdim}
528218887Sdim
529249423Sdimbool Sema::CheckParameterPacksForExpansion(
530249423Sdim    SourceLocation EllipsisLoc, SourceRange PatternRange,
531249423Sdim    ArrayRef<UnexpandedParameterPack> Unexpanded,
532249423Sdim    const MultiLevelTemplateArgumentList &TemplateArgs, bool &ShouldExpand,
533249423Sdim    bool &RetainExpansion, Optional<unsigned> &NumExpansions) {
534218887Sdim  ShouldExpand = true;
535218887Sdim  RetainExpansion = false;
536218887Sdim  std::pair<IdentifierInfo *, SourceLocation> FirstPack;
537218887Sdim  bool HaveFirstPack = false;
538218887Sdim
539226633Sdim  for (ArrayRef<UnexpandedParameterPack>::iterator i = Unexpanded.begin(),
540226633Sdim                                                 end = Unexpanded.end();
541226633Sdim                                                  i != end; ++i) {
542218887Sdim    // Compute the depth and index for this parameter pack.
543218887Sdim    unsigned Depth = 0, Index = 0;
544218887Sdim    IdentifierInfo *Name;
545218887Sdim    bool IsFunctionParameterPack = false;
546218887Sdim
547218887Sdim    if (const TemplateTypeParmType *TTP
548226633Sdim        = i->first.dyn_cast<const TemplateTypeParmType *>()) {
549218887Sdim      Depth = TTP->getDepth();
550218887Sdim      Index = TTP->getIndex();
551221345Sdim      Name = TTP->getIdentifier();
552218887Sdim    } else {
553226633Sdim      NamedDecl *ND = i->first.get<NamedDecl *>();
554218887Sdim      if (isa<ParmVarDecl>(ND))
555218887Sdim        IsFunctionParameterPack = true;
556218887Sdim      else
557218887Sdim        llvm::tie(Depth, Index) = getDepthAndIndex(ND);
558218887Sdim
559218887Sdim      Name = ND->getIdentifier();
560218887Sdim    }
561218887Sdim
562218887Sdim    // Determine the size of this argument pack.
563218887Sdim    unsigned NewPackSize;
564218887Sdim    if (IsFunctionParameterPack) {
565218887Sdim      // Figure out whether we're instantiating to an argument pack or not.
566218887Sdim      typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
567218887Sdim
568218887Sdim      llvm::PointerUnion<Decl *, DeclArgumentPack *> *Instantiation
569218887Sdim        = CurrentInstantiationScope->findInstantiationOf(
570226633Sdim                                        i->first.get<NamedDecl *>());
571218887Sdim      if (Instantiation->is<DeclArgumentPack *>()) {
572218887Sdim        // We could expand this function parameter pack.
573218887Sdim        NewPackSize = Instantiation->get<DeclArgumentPack *>()->size();
574218887Sdim      } else {
575218887Sdim        // We can't expand this function parameter pack, so we can't expand
576218887Sdim        // the pack expansion.
577218887Sdim        ShouldExpand = false;
578218887Sdim        continue;
579218887Sdim      }
580218887Sdim    } else {
581218887Sdim      // If we don't have a template argument at this depth/index, then we
582218887Sdim      // cannot expand the pack expansion. Make a note of this, but we still
583218887Sdim      // want to check any parameter packs we *do* have arguments for.
584218887Sdim      if (Depth >= TemplateArgs.getNumLevels() ||
585218887Sdim          !TemplateArgs.hasTemplateArgument(Depth, Index)) {
586218887Sdim        ShouldExpand = false;
587218887Sdim        continue;
588218887Sdim      }
589218887Sdim
590218887Sdim      // Determine the size of the argument pack.
591218887Sdim      NewPackSize = TemplateArgs(Depth, Index).pack_size();
592218887Sdim    }
593218887Sdim
594218887Sdim    // C++0x [temp.arg.explicit]p9:
595218887Sdim    //   Template argument deduction can extend the sequence of template
596218887Sdim    //   arguments corresponding to a template parameter pack, even when the
597218887Sdim    //   sequence contains explicitly specified template arguments.
598218887Sdim    if (!IsFunctionParameterPack) {
599218887Sdim      if (NamedDecl *PartialPack
600218887Sdim                    = CurrentInstantiationScope->getPartiallySubstitutedPack()){
601218887Sdim        unsigned PartialDepth, PartialIndex;
602218887Sdim        llvm::tie(PartialDepth, PartialIndex) = getDepthAndIndex(PartialPack);
603218887Sdim        if (PartialDepth == Depth && PartialIndex == Index)
604218887Sdim          RetainExpansion = true;
605218887Sdim      }
606218887Sdim    }
607218887Sdim
608218887Sdim    if (!NumExpansions) {
609218887Sdim      // The is the first pack we've seen for which we have an argument.
610218887Sdim      // Record it.
611218887Sdim      NumExpansions = NewPackSize;
612218887Sdim      FirstPack.first = Name;
613226633Sdim      FirstPack.second = i->second;
614218887Sdim      HaveFirstPack = true;
615218887Sdim      continue;
616218887Sdim    }
617218887Sdim
618218887Sdim    if (NewPackSize != *NumExpansions) {
619218887Sdim      // C++0x [temp.variadic]p5:
620218887Sdim      //   All of the parameter packs expanded by a pack expansion shall have
621218887Sdim      //   the same number of arguments specified.
622218887Sdim      if (HaveFirstPack)
623218887Sdim        Diag(EllipsisLoc, diag::err_pack_expansion_length_conflict)
624218887Sdim          << FirstPack.first << Name << *NumExpansions << NewPackSize
625226633Sdim          << SourceRange(FirstPack.second) << SourceRange(i->second);
626218887Sdim      else
627218887Sdim        Diag(EllipsisLoc, diag::err_pack_expansion_length_conflict_multilevel)
628218887Sdim          << Name << *NumExpansions << NewPackSize
629226633Sdim          << SourceRange(i->second);
630218887Sdim      return true;
631218887Sdim    }
632218887Sdim  }
633218887Sdim
634218887Sdim  return false;
635218887Sdim}
636218887Sdim
637249423SdimOptional<unsigned> Sema::getNumArgumentsInExpansion(QualType T,
638218887Sdim                          const MultiLevelTemplateArgumentList &TemplateArgs) {
639218887Sdim  QualType Pattern = cast<PackExpansionType>(T)->getPattern();
640226633Sdim  SmallVector<UnexpandedParameterPack, 2> Unexpanded;
641218887Sdim  CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseType(Pattern);
642218887Sdim
643249423Sdim  Optional<unsigned> Result;
644218887Sdim  for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
645218887Sdim    // Compute the depth and index for this parameter pack.
646218887Sdim    unsigned Depth;
647218887Sdim    unsigned Index;
648218887Sdim
649218887Sdim    if (const TemplateTypeParmType *TTP
650218887Sdim          = Unexpanded[I].first.dyn_cast<const TemplateTypeParmType *>()) {
651218887Sdim      Depth = TTP->getDepth();
652218887Sdim      Index = TTP->getIndex();
653218887Sdim    } else {
654218887Sdim      NamedDecl *ND = Unexpanded[I].first.get<NamedDecl *>();
655218887Sdim      if (isa<ParmVarDecl>(ND)) {
656218887Sdim        // Function parameter pack.
657218887Sdim        typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
658218887Sdim
659218887Sdim        llvm::PointerUnion<Decl *, DeclArgumentPack *> *Instantiation
660218887Sdim          = CurrentInstantiationScope->findInstantiationOf(
661218887Sdim                                        Unexpanded[I].first.get<NamedDecl *>());
662239462Sdim        if (Instantiation->is<Decl*>())
663239462Sdim          // The pattern refers to an unexpanded pack. We're not ready to expand
664239462Sdim          // this pack yet.
665249423Sdim          return None;
666239462Sdim
667239462Sdim        unsigned Size = Instantiation->get<DeclArgumentPack *>()->size();
668239462Sdim        assert((!Result || *Result == Size) && "inconsistent pack sizes");
669239462Sdim        Result = Size;
670218887Sdim        continue;
671218887Sdim      }
672218887Sdim
673218887Sdim      llvm::tie(Depth, Index) = getDepthAndIndex(ND);
674218887Sdim    }
675218887Sdim    if (Depth >= TemplateArgs.getNumLevels() ||
676218887Sdim        !TemplateArgs.hasTemplateArgument(Depth, Index))
677239462Sdim      // The pattern refers to an unknown template argument. We're not ready to
678239462Sdim      // expand this pack yet.
679249423Sdim      return None;
680218887Sdim
681218887Sdim    // Determine the size of the argument pack.
682239462Sdim    unsigned Size = TemplateArgs(Depth, Index).pack_size();
683239462Sdim    assert((!Result || *Result == Size) && "inconsistent pack sizes");
684239462Sdim    Result = Size;
685218887Sdim  }
686218887Sdim
687239462Sdim  return Result;
688218887Sdim}
689218887Sdim
690218887Sdimbool Sema::containsUnexpandedParameterPacks(Declarator &D) {
691218887Sdim  const DeclSpec &DS = D.getDeclSpec();
692218887Sdim  switch (DS.getTypeSpecType()) {
693218887Sdim  case TST_typename:
694223017Sdim  case TST_typeofType:
695226633Sdim  case TST_underlyingType:
696226633Sdim  case TST_atomic: {
697218887Sdim    QualType T = DS.getRepAsType().get();
698218887Sdim    if (!T.isNull() && T->containsUnexpandedParameterPack())
699218887Sdim      return true;
700218887Sdim    break;
701218887Sdim  }
702218887Sdim
703218887Sdim  case TST_typeofExpr:
704218887Sdim  case TST_decltype:
705218887Sdim    if (DS.getRepAsExpr() &&
706218887Sdim        DS.getRepAsExpr()->containsUnexpandedParameterPack())
707218887Sdim      return true;
708218887Sdim    break;
709218887Sdim
710218887Sdim  case TST_unspecified:
711218887Sdim  case TST_void:
712218887Sdim  case TST_char:
713218887Sdim  case TST_wchar:
714218887Sdim  case TST_char16:
715218887Sdim  case TST_char32:
716218887Sdim  case TST_int:
717234353Sdim  case TST_int128:
718226633Sdim  case TST_half:
719218887Sdim  case TST_float:
720218887Sdim  case TST_double:
721218887Sdim  case TST_bool:
722218887Sdim  case TST_decimal32:
723218887Sdim  case TST_decimal64:
724218887Sdim  case TST_decimal128:
725218887Sdim  case TST_enum:
726218887Sdim  case TST_union:
727218887Sdim  case TST_struct:
728243830Sdim  case TST_interface:
729218887Sdim  case TST_class:
730218887Sdim  case TST_auto:
731251662Sdim  case TST_decltype_auto:
732221345Sdim  case TST_unknown_anytype:
733249423Sdim  case TST_image1d_t:
734249423Sdim  case TST_image1d_array_t:
735249423Sdim  case TST_image1d_buffer_t:
736249423Sdim  case TST_image2d_t:
737249423Sdim  case TST_image2d_array_t:
738249423Sdim  case TST_image3d_t:
739249423Sdim  case TST_sampler_t:
740249423Sdim  case TST_event_t:
741218887Sdim  case TST_error:
742218887Sdim    break;
743218887Sdim  }
744218887Sdim
745218887Sdim  for (unsigned I = 0, N = D.getNumTypeObjects(); I != N; ++I) {
746218887Sdim    const DeclaratorChunk &Chunk = D.getTypeObject(I);
747218887Sdim    switch (Chunk.Kind) {
748218887Sdim    case DeclaratorChunk::Pointer:
749218887Sdim    case DeclaratorChunk::Reference:
750218887Sdim    case DeclaratorChunk::Paren:
751218887Sdim      // These declarator chunks cannot contain any parameter packs.
752218887Sdim      break;
753218887Sdim
754218887Sdim    case DeclaratorChunk::Array:
755218887Sdim    case DeclaratorChunk::Function:
756218887Sdim    case DeclaratorChunk::BlockPointer:
757218887Sdim      // Syntactically, these kinds of declarator chunks all come after the
758218887Sdim      // declarator-id (conceptually), so the parser should not invoke this
759218887Sdim      // routine at this time.
760218887Sdim      llvm_unreachable("Could not have seen this kind of declarator chunk");
761218887Sdim
762218887Sdim    case DeclaratorChunk::MemberPointer:
763218887Sdim      if (Chunk.Mem.Scope().getScopeRep() &&
764218887Sdim          Chunk.Mem.Scope().getScopeRep()->containsUnexpandedParameterPack())
765218887Sdim        return true;
766218887Sdim      break;
767218887Sdim    }
768218887Sdim  }
769218887Sdim
770218887Sdim  return false;
771218887Sdim}
772218887Sdim
773234353Sdimnamespace {
774234353Sdim
775234353Sdim// Callback to only accept typo corrections that refer to parameter packs.
776234353Sdimclass ParameterPackValidatorCCC : public CorrectionCandidateCallback {
777234353Sdim public:
778234353Sdim  virtual bool ValidateCandidate(const TypoCorrection &candidate) {
779234353Sdim    NamedDecl *ND = candidate.getCorrectionDecl();
780234353Sdim    return ND && ND->isParameterPack();
781234353Sdim  }
782234353Sdim};
783234353Sdim
784234353Sdim}
785234353Sdim
786218887Sdim/// \brief Called when an expression computing the size of a parameter pack
787218887Sdim/// is parsed.
788218887Sdim///
789218887Sdim/// \code
790218887Sdim/// template<typename ...Types> struct count {
791218887Sdim///   static const unsigned value = sizeof...(Types);
792218887Sdim/// };
793218887Sdim/// \endcode
794218887Sdim///
795218887Sdim//
796218887Sdim/// \param OpLoc The location of the "sizeof" keyword.
797218887Sdim/// \param Name The name of the parameter pack whose size will be determined.
798218887Sdim/// \param NameLoc The source location of the name of the parameter pack.
799218887Sdim/// \param RParenLoc The location of the closing parentheses.
800218887SdimExprResult Sema::ActOnSizeofParameterPackExpr(Scope *S,
801218887Sdim                                              SourceLocation OpLoc,
802218887Sdim                                              IdentifierInfo &Name,
803218887Sdim                                              SourceLocation NameLoc,
804218887Sdim                                              SourceLocation RParenLoc) {
805218887Sdim  // C++0x [expr.sizeof]p5:
806218887Sdim  //   The identifier in a sizeof... expression shall name a parameter pack.
807218887Sdim  LookupResult R(*this, &Name, NameLoc, LookupOrdinaryName);
808218887Sdim  LookupName(R, S);
809218887Sdim
810218887Sdim  NamedDecl *ParameterPack = 0;
811234353Sdim  ParameterPackValidatorCCC Validator;
812218887Sdim  switch (R.getResultKind()) {
813218887Sdim  case LookupResult::Found:
814218887Sdim    ParameterPack = R.getFoundDecl();
815218887Sdim    break;
816218887Sdim
817218887Sdim  case LookupResult::NotFound:
818218887Sdim  case LookupResult::NotFoundInCurrentInstantiation:
819224145Sdim    if (TypoCorrection Corrected = CorrectTypo(R.getLookupNameInfo(),
820234353Sdim                                               R.getLookupKind(), S, 0,
821234353Sdim                                               Validator)) {
822263508Sdim      diagnoseTypo(Corrected,
823263508Sdim                   PDiag(diag::err_sizeof_pack_no_pack_name_suggest) << &Name,
824263508Sdim                   PDiag(diag::note_parameter_pack_here));
825234353Sdim      ParameterPack = Corrected.getCorrectionDecl();
826218887Sdim    }
827263508Sdim
828218887Sdim  case LookupResult::FoundOverloaded:
829218887Sdim  case LookupResult::FoundUnresolvedValue:
830218887Sdim    break;
831218887Sdim
832218887Sdim  case LookupResult::Ambiguous:
833218887Sdim    DiagnoseAmbiguousLookup(R);
834218887Sdim    return ExprError();
835218887Sdim  }
836218887Sdim
837218887Sdim  if (!ParameterPack || !ParameterPack->isParameterPack()) {
838218887Sdim    Diag(NameLoc, diag::err_sizeof_pack_no_pack_name)
839218887Sdim      << &Name;
840218887Sdim    return ExprError();
841218887Sdim  }
842218887Sdim
843249423Sdim  MarkAnyDeclReferenced(OpLoc, ParameterPack, true);
844234353Sdim
845218887Sdim  return new (Context) SizeOfPackExpr(Context.getSizeType(), OpLoc,
846218887Sdim                                      ParameterPack, NameLoc, RParenLoc);
847218887Sdim}
848263508Sdim
849263508SdimTemplateArgumentLoc
850263508SdimSema::getTemplateArgumentPackExpansionPattern(
851263508Sdim      TemplateArgumentLoc OrigLoc,
852263508Sdim      SourceLocation &Ellipsis, Optional<unsigned> &NumExpansions) const {
853263508Sdim  const TemplateArgument &Argument = OrigLoc.getArgument();
854263508Sdim  assert(Argument.isPackExpansion());
855263508Sdim  switch (Argument.getKind()) {
856263508Sdim  case TemplateArgument::Type: {
857263508Sdim    // FIXME: We shouldn't ever have to worry about missing
858263508Sdim    // type-source info!
859263508Sdim    TypeSourceInfo *ExpansionTSInfo = OrigLoc.getTypeSourceInfo();
860263508Sdim    if (!ExpansionTSInfo)
861263508Sdim      ExpansionTSInfo = Context.getTrivialTypeSourceInfo(Argument.getAsType(),
862263508Sdim                                                         Ellipsis);
863263508Sdim    PackExpansionTypeLoc Expansion =
864263508Sdim        ExpansionTSInfo->getTypeLoc().castAs<PackExpansionTypeLoc>();
865263508Sdim    Ellipsis = Expansion.getEllipsisLoc();
866263508Sdim
867263508Sdim    TypeLoc Pattern = Expansion.getPatternLoc();
868263508Sdim    NumExpansions = Expansion.getTypePtr()->getNumExpansions();
869263508Sdim
870263508Sdim    // We need to copy the TypeLoc because TemplateArgumentLocs store a
871263508Sdim    // TypeSourceInfo.
872263508Sdim    // FIXME: Find some way to avoid the copy?
873263508Sdim    TypeLocBuilder TLB;
874263508Sdim    TLB.pushFullCopy(Pattern);
875263508Sdim    TypeSourceInfo *PatternTSInfo =
876263508Sdim        TLB.getTypeSourceInfo(Context, Pattern.getType());
877263508Sdim    return TemplateArgumentLoc(TemplateArgument(Pattern.getType()),
878263508Sdim                               PatternTSInfo);
879263508Sdim  }
880263508Sdim
881263508Sdim  case TemplateArgument::Expression: {
882263508Sdim    PackExpansionExpr *Expansion
883263508Sdim      = cast<PackExpansionExpr>(Argument.getAsExpr());
884263508Sdim    Expr *Pattern = Expansion->getPattern();
885263508Sdim    Ellipsis = Expansion->getEllipsisLoc();
886263508Sdim    NumExpansions = Expansion->getNumExpansions();
887263508Sdim    return TemplateArgumentLoc(Pattern, Pattern);
888263508Sdim  }
889263508Sdim
890263508Sdim  case TemplateArgument::TemplateExpansion:
891263508Sdim    Ellipsis = OrigLoc.getTemplateEllipsisLoc();
892263508Sdim    NumExpansions = Argument.getNumTemplateExpansions();
893263508Sdim    return TemplateArgumentLoc(Argument.getPackExpansionPattern(),
894263508Sdim                               OrigLoc.getTemplateQualifierLoc(),
895263508Sdim                               OrigLoc.getTemplateNameLoc());
896263508Sdim
897263508Sdim  case TemplateArgument::Declaration:
898263508Sdim  case TemplateArgument::NullPtr:
899263508Sdim  case TemplateArgument::Template:
900263508Sdim  case TemplateArgument::Integral:
901263508Sdim  case TemplateArgument::Pack:
902263508Sdim  case TemplateArgument::Null:
903263508Sdim    return TemplateArgumentLoc();
904263508Sdim  }
905263508Sdim
906263508Sdim  llvm_unreachable("Invalid TemplateArgument Kind!");
907263508Sdim}
908