1 //===------- SemaTemplateVariadic.cpp - C++ Variadic Templates ------------===/
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //===----------------------------------------------------------------------===/
9 // This file implements semantic analysis for C++0x variadic templates.
10 //===----------------------------------------------------------------------===/
12 #include "clang/Sema/Sema.h"
13 #include "clang/Sema/Lookup.h"
14 #include "clang/Sema/ParsedTemplate.h"
15 #include "clang/Sema/SemaInternal.h"
16 #include "clang/Sema/Template.h"
17 #include "clang/AST/Expr.h"
18 #include "clang/AST/RecursiveASTVisitor.h"
19 #include "clang/AST/TypeLoc.h"
21 using namespace clang;
23 //----------------------------------------------------------------------------
24 // Visitor that collects unexpanded parameter packs
25 //----------------------------------------------------------------------------
28 /// \brief A class that collects unexpanded parameter packs.
29 class CollectUnexpandedParameterPacksVisitor :
30 public RecursiveASTVisitor<CollectUnexpandedParameterPacksVisitor>
32 typedef RecursiveASTVisitor<CollectUnexpandedParameterPacksVisitor>
35 llvm::SmallVectorImpl<UnexpandedParameterPack> &Unexpanded;
38 explicit CollectUnexpandedParameterPacksVisitor(
39 llvm::SmallVectorImpl<UnexpandedParameterPack> &Unexpanded)
40 : Unexpanded(Unexpanded) { }
42 bool shouldWalkTypesOfTypeLocs() const { return false; }
44 //------------------------------------------------------------------------
45 // Recording occurrences of (unexpanded) parameter packs.
46 //------------------------------------------------------------------------
48 /// \brief Record occurrences of template type parameter packs.
49 bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
50 if (TL.getTypePtr()->isParameterPack())
51 Unexpanded.push_back(std::make_pair(TL.getTypePtr(), TL.getNameLoc()));
55 /// \brief Record occurrences of template type parameter packs
56 /// when we don't have proper source-location information for
59 /// Ideally, this routine would never be used.
60 bool VisitTemplateTypeParmType(TemplateTypeParmType *T) {
61 if (T->isParameterPack())
62 Unexpanded.push_back(std::make_pair(T, SourceLocation()));
67 /// \brief Record occurrences of function and non-type template
68 /// parameter packs in an expression.
69 bool VisitDeclRefExpr(DeclRefExpr *E) {
70 if (E->getDecl()->isParameterPack())
71 Unexpanded.push_back(std::make_pair(E->getDecl(), E->getLocation()));
76 // \brief Record occurrences of function and non-type template parameter
77 // packs in a block-captured expression.
78 bool VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
79 if (E->getDecl()->isParameterPack())
80 Unexpanded.push_back(std::make_pair(E->getDecl(), E->getLocation()));
85 /// \brief Record occurrences of template template parameter packs.
86 bool TraverseTemplateName(TemplateName Template) {
87 if (TemplateTemplateParmDecl *TTP
88 = dyn_cast_or_null<TemplateTemplateParmDecl>(
89 Template.getAsTemplateDecl()))
90 if (TTP->isParameterPack())
91 Unexpanded.push_back(std::make_pair(TTP, SourceLocation()));
93 return inherited::TraverseTemplateName(Template);
96 //------------------------------------------------------------------------
97 // Pruning the search for unexpanded parameter packs.
98 //------------------------------------------------------------------------
100 /// \brief Suppress traversal into statements and expressions that
101 /// do not contain unexpanded parameter packs.
102 bool TraverseStmt(Stmt *S) {
103 if (Expr *E = dyn_cast_or_null<Expr>(S))
104 if (E->containsUnexpandedParameterPack())
105 return inherited::TraverseStmt(E);
110 /// \brief Suppress traversal into types that do not contain
111 /// unexpanded parameter packs.
112 bool TraverseType(QualType T) {
113 if (!T.isNull() && T->containsUnexpandedParameterPack())
114 return inherited::TraverseType(T);
119 /// \brief Suppress traversel into types with location information
120 /// that do not contain unexpanded parameter packs.
121 bool TraverseTypeLoc(TypeLoc TL) {
122 if (!TL.getType().isNull() &&
123 TL.getType()->containsUnexpandedParameterPack())
124 return inherited::TraverseTypeLoc(TL);
129 /// \brief Suppress traversal of non-parameter declarations, since
130 /// they cannot contain unexpanded parameter packs.
131 bool TraverseDecl(Decl *D) {
132 if (D && isa<ParmVarDecl>(D))
133 return inherited::TraverseDecl(D);
138 /// \brief Suppress traversal of template argument pack expansions.
139 bool TraverseTemplateArgument(const TemplateArgument &Arg) {
140 if (Arg.isPackExpansion())
143 return inherited::TraverseTemplateArgument(Arg);
146 /// \brief Suppress traversal of template argument pack expansions.
147 bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) {
148 if (ArgLoc.getArgument().isPackExpansion())
151 return inherited::TraverseTemplateArgumentLoc(ArgLoc);
156 /// \brief Diagnose all of the unexpanded parameter packs in the given
159 DiagnoseUnexpandedParameterPacks(Sema &S, SourceLocation Loc,
160 Sema::UnexpandedParameterPackContext UPPC,
161 const llvm::SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
162 llvm::SmallVector<SourceLocation, 4> Locations;
163 llvm::SmallVector<IdentifierInfo *, 4> Names;
164 llvm::SmallPtrSet<IdentifierInfo *, 4> NamesKnown;
166 for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
167 IdentifierInfo *Name = 0;
168 if (const TemplateTypeParmType *TTP
169 = Unexpanded[I].first.dyn_cast<const TemplateTypeParmType *>())
170 Name = TTP->getIdentifier();
172 Name = Unexpanded[I].first.get<NamedDecl *>()->getIdentifier();
174 if (Name && NamesKnown.insert(Name))
175 Names.push_back(Name);
177 if (Unexpanded[I].second.isValid())
178 Locations.push_back(Unexpanded[I].second);
182 = Names.size() == 0? S.Diag(Loc, diag::err_unexpanded_parameter_pack_0)
184 : Names.size() == 1? S.Diag(Loc, diag::err_unexpanded_parameter_pack_1)
185 << (int)UPPC << Names[0]
186 : Names.size() == 2? S.Diag(Loc, diag::err_unexpanded_parameter_pack_2)
187 << (int)UPPC << Names[0] << Names[1]
188 : S.Diag(Loc, diag::err_unexpanded_parameter_pack_3_or_more)
189 << (int)UPPC << Names[0] << Names[1];
191 for (unsigned I = 0, N = Locations.size(); I != N; ++I)
192 DB << SourceRange(Locations[I]);
195 bool Sema::DiagnoseUnexpandedParameterPack(SourceLocation Loc,
197 UnexpandedParameterPackContext UPPC) {
198 // C++0x [temp.variadic]p5:
199 // An appearance of a name of a parameter pack that is not expanded is
201 if (!T->getType()->containsUnexpandedParameterPack())
204 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
205 CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseTypeLoc(
207 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
208 DiagnoseUnexpandedParameterPacks(*this, Loc, UPPC, Unexpanded);
212 bool Sema::DiagnoseUnexpandedParameterPack(Expr *E,
213 UnexpandedParameterPackContext UPPC) {
214 // C++0x [temp.variadic]p5:
215 // An appearance of a name of a parameter pack that is not expanded is
217 if (!E->containsUnexpandedParameterPack())
220 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
221 CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseStmt(E);
222 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
223 DiagnoseUnexpandedParameterPacks(*this, E->getLocStart(), UPPC, Unexpanded);
227 bool Sema::DiagnoseUnexpandedParameterPack(const CXXScopeSpec &SS,
228 UnexpandedParameterPackContext UPPC) {
229 // C++0x [temp.variadic]p5:
230 // An appearance of a name of a parameter pack that is not expanded is
232 if (!SS.getScopeRep() ||
233 !SS.getScopeRep()->containsUnexpandedParameterPack())
236 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
237 CollectUnexpandedParameterPacksVisitor(Unexpanded)
238 .TraverseNestedNameSpecifier(SS.getScopeRep());
239 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
240 DiagnoseUnexpandedParameterPacks(*this, SS.getRange().getBegin(),
245 bool Sema::DiagnoseUnexpandedParameterPack(const DeclarationNameInfo &NameInfo,
246 UnexpandedParameterPackContext UPPC) {
247 // C++0x [temp.variadic]p5:
248 // An appearance of a name of a parameter pack that is not expanded is
250 switch (NameInfo.getName().getNameKind()) {
251 case DeclarationName::Identifier:
252 case DeclarationName::ObjCZeroArgSelector:
253 case DeclarationName::ObjCOneArgSelector:
254 case DeclarationName::ObjCMultiArgSelector:
255 case DeclarationName::CXXOperatorName:
256 case DeclarationName::CXXLiteralOperatorName:
257 case DeclarationName::CXXUsingDirective:
260 case DeclarationName::CXXConstructorName:
261 case DeclarationName::CXXDestructorName:
262 case DeclarationName::CXXConversionFunctionName:
263 // FIXME: We shouldn't need this null check!
264 if (TypeSourceInfo *TSInfo = NameInfo.getNamedTypeInfo())
265 return DiagnoseUnexpandedParameterPack(NameInfo.getLoc(), TSInfo, UPPC);
267 if (!NameInfo.getName().getCXXNameType()->containsUnexpandedParameterPack())
273 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
274 CollectUnexpandedParameterPacksVisitor(Unexpanded)
275 .TraverseType(NameInfo.getName().getCXXNameType());
276 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
277 DiagnoseUnexpandedParameterPacks(*this, NameInfo.getLoc(), UPPC, Unexpanded);
281 bool Sema::DiagnoseUnexpandedParameterPack(SourceLocation Loc,
282 TemplateName Template,
283 UnexpandedParameterPackContext UPPC) {
285 if (Template.isNull() || !Template.containsUnexpandedParameterPack())
288 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
289 CollectUnexpandedParameterPacksVisitor(Unexpanded)
290 .TraverseTemplateName(Template);
291 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
292 DiagnoseUnexpandedParameterPacks(*this, Loc, UPPC, Unexpanded);
296 bool Sema::DiagnoseUnexpandedParameterPack(TemplateArgumentLoc Arg,
297 UnexpandedParameterPackContext UPPC) {
298 if (Arg.getArgument().isNull() ||
299 !Arg.getArgument().containsUnexpandedParameterPack())
302 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
303 CollectUnexpandedParameterPacksVisitor(Unexpanded)
304 .TraverseTemplateArgumentLoc(Arg);
305 assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
306 DiagnoseUnexpandedParameterPacks(*this, Arg.getLocation(), UPPC, Unexpanded);
310 void Sema::collectUnexpandedParameterPacks(TemplateArgument Arg,
311 llvm::SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
312 CollectUnexpandedParameterPacksVisitor(Unexpanded)
313 .TraverseTemplateArgument(Arg);
316 void Sema::collectUnexpandedParameterPacks(TemplateArgumentLoc Arg,
317 llvm::SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
318 CollectUnexpandedParameterPacksVisitor(Unexpanded)
319 .TraverseTemplateArgumentLoc(Arg);
322 void Sema::collectUnexpandedParameterPacks(QualType T,
323 llvm::SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
324 CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseType(T);
327 void Sema::collectUnexpandedParameterPacks(TypeLoc TL,
328 llvm::SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
329 CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseTypeLoc(TL);
332 ParsedTemplateArgument
333 Sema::ActOnPackExpansion(const ParsedTemplateArgument &Arg,
334 SourceLocation EllipsisLoc) {
338 switch (Arg.getKind()) {
339 case ParsedTemplateArgument::Type: {
340 TypeResult Result = ActOnPackExpansion(Arg.getAsType(), EllipsisLoc);
341 if (Result.isInvalid())
342 return ParsedTemplateArgument();
344 return ParsedTemplateArgument(Arg.getKind(), Result.get().getAsOpaquePtr(),
348 case ParsedTemplateArgument::NonType: {
349 ExprResult Result = ActOnPackExpansion(Arg.getAsExpr(), EllipsisLoc);
350 if (Result.isInvalid())
351 return ParsedTemplateArgument();
353 return ParsedTemplateArgument(Arg.getKind(), Result.get(),
357 case ParsedTemplateArgument::Template:
358 if (!Arg.getAsTemplate().get().containsUnexpandedParameterPack()) {
359 SourceRange R(Arg.getLocation());
360 if (Arg.getScopeSpec().isValid())
361 R.setBegin(Arg.getScopeSpec().getBeginLoc());
362 Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs)
364 return ParsedTemplateArgument();
367 return Arg.getTemplatePackExpansion(EllipsisLoc);
369 llvm_unreachable("Unhandled template argument kind?");
370 return ParsedTemplateArgument();
373 TypeResult Sema::ActOnPackExpansion(ParsedType Type,
374 SourceLocation EllipsisLoc) {
375 TypeSourceInfo *TSInfo;
376 GetTypeFromParser(Type, &TSInfo);
380 TypeSourceInfo *TSResult = CheckPackExpansion(TSInfo, EllipsisLoc,
381 llvm::Optional<unsigned>());
385 return CreateParsedType(TSResult->getType(), TSResult);
388 TypeSourceInfo *Sema::CheckPackExpansion(TypeSourceInfo *Pattern,
389 SourceLocation EllipsisLoc,
390 llvm::Optional<unsigned> NumExpansions) {
391 // Create the pack expansion type and source-location information.
392 QualType Result = CheckPackExpansion(Pattern->getType(),
393 Pattern->getTypeLoc().getSourceRange(),
394 EllipsisLoc, NumExpansions);
398 TypeSourceInfo *TSResult = Context.CreateTypeSourceInfo(Result);
399 PackExpansionTypeLoc TL = cast<PackExpansionTypeLoc>(TSResult->getTypeLoc());
400 TL.setEllipsisLoc(EllipsisLoc);
402 // Copy over the source-location information from the type.
403 memcpy(TL.getNextTypeLoc().getOpaqueData(),
404 Pattern->getTypeLoc().getOpaqueData(),
405 Pattern->getTypeLoc().getFullDataSize());
409 QualType Sema::CheckPackExpansion(QualType Pattern,
410 SourceRange PatternRange,
411 SourceLocation EllipsisLoc,
412 llvm::Optional<unsigned> NumExpansions) {
413 // C++0x [temp.variadic]p5:
414 // The pattern of a pack expansion shall name one or more
415 // parameter packs that are not expanded by a nested pack
417 if (!Pattern->containsUnexpandedParameterPack()) {
418 Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs)
423 return Context.getPackExpansionType(Pattern, NumExpansions);
426 ExprResult Sema::ActOnPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc) {
427 return CheckPackExpansion(Pattern, EllipsisLoc, llvm::Optional<unsigned>());
430 ExprResult Sema::CheckPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc,
431 llvm::Optional<unsigned> NumExpansions) {
435 // C++0x [temp.variadic]p5:
436 // The pattern of a pack expansion shall name one or more
437 // parameter packs that are not expanded by a nested pack
439 if (!Pattern->containsUnexpandedParameterPack()) {
440 Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs)
441 << Pattern->getSourceRange();
445 // Create the pack expansion expression and source-location information.
446 return Owned(new (Context) PackExpansionExpr(Context.DependentTy, Pattern,
447 EllipsisLoc, NumExpansions));
450 /// \brief Retrieve the depth and index of a parameter pack.
451 static std::pair<unsigned, unsigned>
452 getDepthAndIndex(NamedDecl *ND) {
453 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND))
454 return std::make_pair(TTP->getDepth(), TTP->getIndex());
456 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND))
457 return std::make_pair(NTTP->getDepth(), NTTP->getIndex());
459 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND);
460 return std::make_pair(TTP->getDepth(), TTP->getIndex());
463 bool Sema::CheckParameterPacksForExpansion(SourceLocation EllipsisLoc,
464 SourceRange PatternRange,
465 const UnexpandedParameterPack *Unexpanded,
466 unsigned NumUnexpanded,
467 const MultiLevelTemplateArgumentList &TemplateArgs,
469 bool &RetainExpansion,
470 llvm::Optional<unsigned> &NumExpansions) {
472 RetainExpansion = false;
473 std::pair<IdentifierInfo *, SourceLocation> FirstPack;
474 bool HaveFirstPack = false;
476 for (unsigned I = 0; I != NumUnexpanded; ++I) {
477 // Compute the depth and index for this parameter pack.
478 unsigned Depth = 0, Index = 0;
479 IdentifierInfo *Name;
480 bool IsFunctionParameterPack = false;
482 if (const TemplateTypeParmType *TTP
483 = Unexpanded[I].first.dyn_cast<const TemplateTypeParmType *>()) {
484 Depth = TTP->getDepth();
485 Index = TTP->getIndex();
486 Name = TTP->getIdentifier();
488 NamedDecl *ND = Unexpanded[I].first.get<NamedDecl *>();
489 if (isa<ParmVarDecl>(ND))
490 IsFunctionParameterPack = true;
492 llvm::tie(Depth, Index) = getDepthAndIndex(ND);
494 Name = ND->getIdentifier();
497 // Determine the size of this argument pack.
498 unsigned NewPackSize;
499 if (IsFunctionParameterPack) {
500 // Figure out whether we're instantiating to an argument pack or not.
501 typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
503 llvm::PointerUnion<Decl *, DeclArgumentPack *> *Instantiation
504 = CurrentInstantiationScope->findInstantiationOf(
505 Unexpanded[I].first.get<NamedDecl *>());
506 if (Instantiation->is<DeclArgumentPack *>()) {
507 // We could expand this function parameter pack.
508 NewPackSize = Instantiation->get<DeclArgumentPack *>()->size();
510 // We can't expand this function parameter pack, so we can't expand
511 // the pack expansion.
512 ShouldExpand = false;
516 // If we don't have a template argument at this depth/index, then we
517 // cannot expand the pack expansion. Make a note of this, but we still
518 // want to check any parameter packs we *do* have arguments for.
519 if (Depth >= TemplateArgs.getNumLevels() ||
520 !TemplateArgs.hasTemplateArgument(Depth, Index)) {
521 ShouldExpand = false;
525 // Determine the size of the argument pack.
526 NewPackSize = TemplateArgs(Depth, Index).pack_size();
529 // C++0x [temp.arg.explicit]p9:
530 // Template argument deduction can extend the sequence of template
531 // arguments corresponding to a template parameter pack, even when the
532 // sequence contains explicitly specified template arguments.
533 if (!IsFunctionParameterPack) {
534 if (NamedDecl *PartialPack
535 = CurrentInstantiationScope->getPartiallySubstitutedPack()){
536 unsigned PartialDepth, PartialIndex;
537 llvm::tie(PartialDepth, PartialIndex) = getDepthAndIndex(PartialPack);
538 if (PartialDepth == Depth && PartialIndex == Index)
539 RetainExpansion = true;
543 if (!NumExpansions) {
544 // The is the first pack we've seen for which we have an argument.
546 NumExpansions = NewPackSize;
547 FirstPack.first = Name;
548 FirstPack.second = Unexpanded[I].second;
549 HaveFirstPack = true;
553 if (NewPackSize != *NumExpansions) {
554 // C++0x [temp.variadic]p5:
555 // All of the parameter packs expanded by a pack expansion shall have
556 // the same number of arguments specified.
558 Diag(EllipsisLoc, diag::err_pack_expansion_length_conflict)
559 << FirstPack.first << Name << *NumExpansions << NewPackSize
560 << SourceRange(FirstPack.second) << SourceRange(Unexpanded[I].second);
562 Diag(EllipsisLoc, diag::err_pack_expansion_length_conflict_multilevel)
563 << Name << *NumExpansions << NewPackSize
564 << SourceRange(Unexpanded[I].second);
572 unsigned Sema::getNumArgumentsInExpansion(QualType T,
573 const MultiLevelTemplateArgumentList &TemplateArgs) {
574 QualType Pattern = cast<PackExpansionType>(T)->getPattern();
575 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
576 CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseType(Pattern);
578 for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
579 // Compute the depth and index for this parameter pack.
583 if (const TemplateTypeParmType *TTP
584 = Unexpanded[I].first.dyn_cast<const TemplateTypeParmType *>()) {
585 Depth = TTP->getDepth();
586 Index = TTP->getIndex();
588 NamedDecl *ND = Unexpanded[I].first.get<NamedDecl *>();
589 if (isa<ParmVarDecl>(ND)) {
590 // Function parameter pack.
591 typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
593 llvm::PointerUnion<Decl *, DeclArgumentPack *> *Instantiation
594 = CurrentInstantiationScope->findInstantiationOf(
595 Unexpanded[I].first.get<NamedDecl *>());
596 if (Instantiation->is<DeclArgumentPack *>())
597 return Instantiation->get<DeclArgumentPack *>()->size();
602 llvm::tie(Depth, Index) = getDepthAndIndex(ND);
604 if (Depth >= TemplateArgs.getNumLevels() ||
605 !TemplateArgs.hasTemplateArgument(Depth, Index))
608 // Determine the size of the argument pack.
609 return TemplateArgs(Depth, Index).pack_size();
612 llvm_unreachable("No unexpanded parameter packs in type expansion.");
616 bool Sema::containsUnexpandedParameterPacks(Declarator &D) {
617 const DeclSpec &DS = D.getDeclSpec();
618 switch (DS.getTypeSpecType()) {
621 case TST_underlyingType: {
622 QualType T = DS.getRepAsType().get();
623 if (!T.isNull() && T->containsUnexpandedParameterPack())
630 if (DS.getRepAsExpr() &&
631 DS.getRepAsExpr()->containsUnexpandedParameterPack())
635 case TST_unspecified:
653 case TST_unknown_anytype:
658 for (unsigned I = 0, N = D.getNumTypeObjects(); I != N; ++I) {
659 const DeclaratorChunk &Chunk = D.getTypeObject(I);
660 switch (Chunk.Kind) {
661 case DeclaratorChunk::Pointer:
662 case DeclaratorChunk::Reference:
663 case DeclaratorChunk::Paren:
664 // These declarator chunks cannot contain any parameter packs.
667 case DeclaratorChunk::Array:
668 case DeclaratorChunk::Function:
669 case DeclaratorChunk::BlockPointer:
670 // Syntactically, these kinds of declarator chunks all come after the
671 // declarator-id (conceptually), so the parser should not invoke this
672 // routine at this time.
673 llvm_unreachable("Could not have seen this kind of declarator chunk");
676 case DeclaratorChunk::MemberPointer:
677 if (Chunk.Mem.Scope().getScopeRep() &&
678 Chunk.Mem.Scope().getScopeRep()->containsUnexpandedParameterPack())
687 /// \brief Called when an expression computing the size of a parameter pack
691 /// template<typename ...Types> struct count {
692 /// static const unsigned value = sizeof...(Types);
697 /// \param OpLoc The location of the "sizeof" keyword.
698 /// \param Name The name of the parameter pack whose size will be determined.
699 /// \param NameLoc The source location of the name of the parameter pack.
700 /// \param RParenLoc The location of the closing parentheses.
701 ExprResult Sema::ActOnSizeofParameterPackExpr(Scope *S,
702 SourceLocation OpLoc,
703 IdentifierInfo &Name,
704 SourceLocation NameLoc,
705 SourceLocation RParenLoc) {
706 // C++0x [expr.sizeof]p5:
707 // The identifier in a sizeof... expression shall name a parameter pack.
708 LookupResult R(*this, &Name, NameLoc, LookupOrdinaryName);
711 NamedDecl *ParameterPack = 0;
712 switch (R.getResultKind()) {
713 case LookupResult::Found:
714 ParameterPack = R.getFoundDecl();
717 case LookupResult::NotFound:
718 case LookupResult::NotFoundInCurrentInstantiation:
719 if (TypoCorrection Corrected = CorrectTypo(R.getLookupNameInfo(),
720 R.getLookupKind(), S, 0, 0,
721 false, CTC_NoKeywords)) {
722 if (NamedDecl *CorrectedResult = Corrected.getCorrectionDecl())
723 if (CorrectedResult->isParameterPack()) {
724 std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOptions()));
725 ParameterPack = CorrectedResult;
726 Diag(NameLoc, diag::err_sizeof_pack_no_pack_name_suggest)
727 << &Name << CorrectedQuotedStr
728 << FixItHint::CreateReplacement(
729 NameLoc, Corrected.getAsString(getLangOptions()));
730 Diag(ParameterPack->getLocation(), diag::note_parameter_pack_here)
731 << CorrectedQuotedStr;
735 case LookupResult::FoundOverloaded:
736 case LookupResult::FoundUnresolvedValue:
739 case LookupResult::Ambiguous:
740 DiagnoseAmbiguousLookup(R);
744 if (!ParameterPack || !ParameterPack->isParameterPack()) {
745 Diag(NameLoc, diag::err_sizeof_pack_no_pack_name)
750 return new (Context) SizeOfPackExpr(Context.getSizeType(), OpLoc,
751 ParameterPack, NameLoc, RParenLoc);