Douglas Gregor | c463335 | 2010-12-15 17:38:57 +0000 | [diff] [blame] | 1 | //===------- SemaTemplateVariadic.cpp - C++ Variadic Templates ------------===/ |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | //===----------------------------------------------------------------------===/ |
| 8 | // |
| 9 | // This file implements semantic analysis for C++0x variadic templates. |
| 10 | //===----------------------------------------------------------------------===/ |
| 11 | |
| 12 | #include "clang/Sema/Sema.h" |
| 13 | #include "clang/Sema/SemaInternal.h" |
| 14 | #include "clang/AST/Expr.h" |
Douglas Gregor | 9ef7589 | 2010-12-15 19:43:21 +0000 | [diff] [blame^] | 15 | #include "clang/AST/RecursiveASTVisitor.h" |
Douglas Gregor | c463335 | 2010-12-15 17:38:57 +0000 | [diff] [blame] | 16 | #include "clang/AST/TypeLoc.h" |
| 17 | |
| 18 | using namespace clang; |
| 19 | |
Douglas Gregor | 9ef7589 | 2010-12-15 19:43:21 +0000 | [diff] [blame^] | 20 | //---------------------------------------------------------------------------- |
| 21 | // Visitor that collects unexpanded parameter packs |
| 22 | //---------------------------------------------------------------------------- |
| 23 | |
| 24 | // FIXME: No way to easily map from TemplateTypeParmTypes to |
| 25 | // TemplateTypeParmDecls, so we have this horrible PointerUnion. |
| 26 | typedef std::pair<llvm::PointerUnion<const TemplateTypeParmType*, NamedDecl*>, |
| 27 | SourceLocation> UnexpandedParameterPack; |
| 28 | |
| 29 | namespace { |
| 30 | /// \brief A class that collects unexpanded parameter packs. |
| 31 | class CollectUnexpandedParameterPacksVisitor : |
| 32 | public RecursiveASTVisitor<CollectUnexpandedParameterPacksVisitor> |
| 33 | { |
| 34 | typedef RecursiveASTVisitor<CollectUnexpandedParameterPacksVisitor> |
| 35 | inherited; |
| 36 | |
| 37 | llvm::SmallVectorImpl<UnexpandedParameterPack> &Unexpanded; |
| 38 | |
| 39 | public: |
| 40 | explicit CollectUnexpandedParameterPacksVisitor( |
| 41 | llvm::SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) |
| 42 | : Unexpanded(Unexpanded) { } |
| 43 | |
| 44 | //------------------------------------------------------------------------ |
| 45 | // Recording occurrences of (unexpanded) parameter packs. |
| 46 | //------------------------------------------------------------------------ |
| 47 | |
| 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())); |
| 52 | return true; |
| 53 | } |
| 54 | |
| 55 | /// \brief Record occurrences of template type parameter packs |
| 56 | /// when we don't have proper source-location information for |
| 57 | /// them. |
| 58 | /// |
| 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())); |
| 63 | |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | // FIXME: Record occurrences of non-type and template template |
| 68 | // parameter packs. |
| 69 | |
| 70 | // FIXME: Once we have pack expansions in the AST, block their |
| 71 | // traversal. |
| 72 | |
| 73 | //------------------------------------------------------------------------ |
| 74 | // Pruning the search for unexpanded parameter packs. |
| 75 | //------------------------------------------------------------------------ |
| 76 | |
| 77 | /// \brief Suppress traversal into statements and expressions that |
| 78 | /// do not contain unexpanded parameter packs. |
| 79 | bool TraverseStmt(Stmt *S) { |
| 80 | if (Expr *E = dyn_cast_or_null<Expr>(S)) |
| 81 | if (E->containsUnexpandedParameterPack()) |
| 82 | return inherited::TraverseStmt(E); |
| 83 | |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | /// \brief Suppress traversal into types that do not contain |
| 88 | /// unexpanded parameter packs. |
| 89 | bool TraverseType(QualType T) { |
| 90 | if (!T.isNull() && T->containsUnexpandedParameterPack()) |
| 91 | return inherited::TraverseType(T); |
| 92 | |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | /// \brief Suppress traversel into types with location information |
| 97 | /// that do not contain unexpanded parameter packs. |
| 98 | bool TraverseTypeLoc(TypeLoc TL) { |
| 99 | if (!TL.getType().isNull() && TL. |
| 100 | getType()->containsUnexpandedParameterPack()) |
| 101 | return inherited::TraverseTypeLoc(TL); |
| 102 | |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | /// \brief Suppress traversal of declarations, since they cannot |
| 107 | /// contain unexpanded parameter packs. |
| 108 | bool TraverseDecl(Decl *D) { return true; } |
| 109 | }; |
| 110 | } |
| 111 | |
| 112 | /// \brief Diagnose all of the unexpanded parameter packs in the given |
| 113 | /// vector. |
| 114 | static void |
| 115 | DiagnoseUnexpandedParameterPacks(Sema &S, SourceLocation Loc, |
| 116 | Sema::UnexpandedParameterPackContext UPPC, |
| 117 | const llvm::SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) { |
| 118 | llvm::SmallVector<SourceLocation, 4> Locations; |
| 119 | llvm::SmallVector<IdentifierInfo *, 4> Names; |
| 120 | llvm::SmallPtrSet<IdentifierInfo *, 4> NamesKnown; |
| 121 | |
| 122 | for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) { |
| 123 | IdentifierInfo *Name = 0; |
| 124 | if (const TemplateTypeParmType *TTP |
| 125 | = Unexpanded[I].first.dyn_cast<const TemplateTypeParmType *>()) |
| 126 | Name = TTP->getName(); |
| 127 | else |
| 128 | Name = Unexpanded[I].first.get<NamedDecl *>()->getIdentifier(); |
| 129 | |
| 130 | if (Name && NamesKnown.insert(Name)) |
| 131 | Names.push_back(Name); |
| 132 | |
| 133 | if (Unexpanded[I].second.isValid()) |
| 134 | Locations.push_back(Unexpanded[I].second); |
| 135 | } |
| 136 | |
| 137 | DiagnosticBuilder DB |
| 138 | = Names.size() == 0? S.Diag(Loc, diag::err_unexpanded_parameter_pack_0) |
| 139 | << (int)UPPC |
| 140 | : Names.size() == 1? S.Diag(Loc, diag::err_unexpanded_parameter_pack_1) |
| 141 | << (int)UPPC << Names[0] |
| 142 | : Names.size() == 2? S.Diag(Loc, diag::err_unexpanded_parameter_pack_2) |
| 143 | << (int)UPPC << Names[0] << Names[1] |
| 144 | : S.Diag(Loc, diag::err_unexpanded_parameter_pack_3_or_more) |
| 145 | << (int)UPPC << Names[0] << Names[1]; |
| 146 | |
| 147 | for (unsigned I = 0, N = Locations.size(); I != N; ++I) |
| 148 | DB << SourceRange(Locations[I]); |
| 149 | } |
| 150 | |
Douglas Gregor | c463335 | 2010-12-15 17:38:57 +0000 | [diff] [blame] | 151 | bool Sema::DiagnoseUnexpandedParameterPack(SourceLocation Loc, |
| 152 | TypeSourceInfo *T, |
| 153 | UnexpandedParameterPackContext UPPC) { |
| 154 | // C++0x [temp.variadic]p5: |
| 155 | // An appearance of a name of a parameter pack that is not expanded is |
| 156 | // ill-formed. |
| 157 | if (!T->getType()->containsUnexpandedParameterPack()) |
| 158 | return false; |
| 159 | |
Douglas Gregor | 9ef7589 | 2010-12-15 19:43:21 +0000 | [diff] [blame^] | 160 | llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 161 | CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseTypeLoc( |
| 162 | T->getTypeLoc()); |
| 163 | assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs"); |
| 164 | DiagnoseUnexpandedParameterPacks(*this, Loc, UPPC, Unexpanded); |
Douglas Gregor | c463335 | 2010-12-15 17:38:57 +0000 | [diff] [blame] | 165 | return true; |
| 166 | } |
| 167 | |
| 168 | bool Sema::DiagnoseUnexpandedParameterPack(Expr *E, |
| 169 | UnexpandedParameterPackContext UPPC) { |
| 170 | // C++0x [temp.variadic]p5: |
| 171 | // An appearance of a name of a parameter pack that is not expanded is |
| 172 | // ill-formed. |
| 173 | if (!E->containsUnexpandedParameterPack()) |
| 174 | return false; |
| 175 | |
Douglas Gregor | 9ef7589 | 2010-12-15 19:43:21 +0000 | [diff] [blame^] | 176 | llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 177 | CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseStmt(E); |
| 178 | assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs"); |
| 179 | DiagnoseUnexpandedParameterPacks(*this, E->getLocStart(), UPPC, Unexpanded); |
Douglas Gregor | c463335 | 2010-12-15 17:38:57 +0000 | [diff] [blame] | 180 | return true; |
| 181 | } |