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" |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 13 | #include "clang/Sema/Lookup.h" |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 14 | #include "clang/Sema/ParsedTemplate.h" |
Douglas Gregor | c463335 | 2010-12-15 17:38:57 +0000 | [diff] [blame] | 15 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 16 | #include "clang/Sema/Template.h" |
Douglas Gregor | c463335 | 2010-12-15 17:38:57 +0000 | [diff] [blame] | 17 | #include "clang/AST/Expr.h" |
Douglas Gregor | 9ef7589 | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 18 | #include "clang/AST/RecursiveASTVisitor.h" |
Douglas Gregor | c463335 | 2010-12-15 17:38:57 +0000 | [diff] [blame] | 19 | #include "clang/AST/TypeLoc.h" |
| 20 | |
| 21 | using namespace clang; |
| 22 | |
Douglas Gregor | 9ef7589 | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 23 | //---------------------------------------------------------------------------- |
| 24 | // Visitor that collects unexpanded parameter packs |
| 25 | //---------------------------------------------------------------------------- |
| 26 | |
Douglas Gregor | 9ef7589 | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 27 | namespace { |
| 28 | /// \brief A class that collects unexpanded parameter packs. |
| 29 | class CollectUnexpandedParameterPacksVisitor : |
| 30 | public RecursiveASTVisitor<CollectUnexpandedParameterPacksVisitor> |
| 31 | { |
| 32 | typedef RecursiveASTVisitor<CollectUnexpandedParameterPacksVisitor> |
| 33 | inherited; |
| 34 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 35 | SmallVectorImpl<UnexpandedParameterPack> &Unexpanded; |
Douglas Gregor | 9ef7589 | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 36 | |
| 37 | public: |
| 38 | explicit CollectUnexpandedParameterPacksVisitor( |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 39 | SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) |
Douglas Gregor | 9ef7589 | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 40 | : Unexpanded(Unexpanded) { } |
| 41 | |
Douglas Gregor | a40bc72 | 2010-12-20 23:07:20 +0000 | [diff] [blame] | 42 | bool shouldWalkTypesOfTypeLocs() const { return false; } |
| 43 | |
Douglas Gregor | 9ef7589 | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 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 | |
Douglas Gregor | a779d9c | 2011-01-19 21:32:01 +0000 | [diff] [blame] | 67 | /// \brief Record occurrences of function and non-type template |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 68 | /// parameter packs in an expression. |
| 69 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
Douglas Gregor | 12c9c00 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 70 | if (E->getDecl()->isParameterPack()) |
| 71 | Unexpanded.push_back(std::make_pair(E->getDecl(), E->getLocation())); |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 72 | |
| 73 | return true; |
| 74 | } |
| 75 | |
Douglas Gregor | a779d9c | 2011-01-19 21:32:01 +0000 | [diff] [blame] | 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())); |
| 81 | |
| 82 | return true; |
| 83 | } |
| 84 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 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())); |
| 92 | |
| 93 | return inherited::TraverseTemplateName(Template); |
| 94 | } |
Douglas Gregor | 9ef7589 | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 95 | |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 96 | /// \brief Suppress traversal into Objective-C container literal |
| 97 | /// elements that are pack expansions. |
| 98 | bool TraverseObjCDictionaryLiteral(ObjCDictionaryLiteral *E) { |
| 99 | if (!E->containsUnexpandedParameterPack()) |
| 100 | return true; |
| 101 | |
| 102 | for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) { |
| 103 | ObjCDictionaryElement Element = E->getKeyValueElement(I); |
| 104 | if (Element.isPackExpansion()) |
| 105 | continue; |
| 106 | |
| 107 | TraverseStmt(Element.Key); |
| 108 | TraverseStmt(Element.Value); |
| 109 | } |
| 110 | return true; |
| 111 | } |
Douglas Gregor | 9ef7589 | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 112 | //------------------------------------------------------------------------ |
| 113 | // Pruning the search for unexpanded parameter packs. |
| 114 | //------------------------------------------------------------------------ |
| 115 | |
| 116 | /// \brief Suppress traversal into statements and expressions that |
| 117 | /// do not contain unexpanded parameter packs. |
| 118 | bool TraverseStmt(Stmt *S) { |
| 119 | if (Expr *E = dyn_cast_or_null<Expr>(S)) |
| 120 | if (E->containsUnexpandedParameterPack()) |
| 121 | return inherited::TraverseStmt(E); |
| 122 | |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | /// \brief Suppress traversal into types that do not contain |
| 127 | /// unexpanded parameter packs. |
| 128 | bool TraverseType(QualType T) { |
| 129 | if (!T.isNull() && T->containsUnexpandedParameterPack()) |
| 130 | return inherited::TraverseType(T); |
| 131 | |
| 132 | return true; |
| 133 | } |
| 134 | |
| 135 | /// \brief Suppress traversel into types with location information |
| 136 | /// that do not contain unexpanded parameter packs. |
| 137 | bool TraverseTypeLoc(TypeLoc TL) { |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 138 | if (!TL.getType().isNull() && |
| 139 | TL.getType()->containsUnexpandedParameterPack()) |
Douglas Gregor | 9ef7589 | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 140 | return inherited::TraverseTypeLoc(TL); |
| 141 | |
| 142 | return true; |
| 143 | } |
| 144 | |
Douglas Gregor | cff163e | 2010-12-15 21:57:59 +0000 | [diff] [blame] | 145 | /// \brief Suppress traversal of non-parameter declarations, since |
| 146 | /// they cannot contain unexpanded parameter packs. |
| 147 | bool TraverseDecl(Decl *D) { |
| 148 | if (D && isa<ParmVarDecl>(D)) |
| 149 | return inherited::TraverseDecl(D); |
| 150 | |
| 151 | return true; |
| 152 | } |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 153 | |
| 154 | /// \brief Suppress traversal of template argument pack expansions. |
| 155 | bool TraverseTemplateArgument(const TemplateArgument &Arg) { |
| 156 | if (Arg.isPackExpansion()) |
| 157 | return true; |
| 158 | |
| 159 | return inherited::TraverseTemplateArgument(Arg); |
| 160 | } |
| 161 | |
| 162 | /// \brief Suppress traversal of template argument pack expansions. |
| 163 | bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) { |
| 164 | if (ArgLoc.getArgument().isPackExpansion()) |
| 165 | return true; |
| 166 | |
| 167 | return inherited::TraverseTemplateArgumentLoc(ArgLoc); |
| 168 | } |
Douglas Gregor | 9ef7589 | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 169 | }; |
| 170 | } |
| 171 | |
| 172 | /// \brief Diagnose all of the unexpanded parameter packs in the given |
| 173 | /// vector. |
Douglas Gregor | 65019ac | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 174 | void |
| 175 | Sema::DiagnoseUnexpandedParameterPacks(SourceLocation Loc, |
| 176 | UnexpandedParameterPackContext UPPC, |
Bill Wendling | 4fe5be0 | 2012-02-22 09:38:11 +0000 | [diff] [blame] | 177 | ArrayRef<UnexpandedParameterPack> Unexpanded) { |
Douglas Gregor | 65019ac | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 178 | if (Unexpanded.empty()) |
| 179 | return; |
| 180 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 181 | SmallVector<SourceLocation, 4> Locations; |
| 182 | SmallVector<IdentifierInfo *, 4> Names; |
Douglas Gregor | 9ef7589 | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 183 | llvm::SmallPtrSet<IdentifierInfo *, 4> NamesKnown; |
| 184 | |
| 185 | for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) { |
| 186 | IdentifierInfo *Name = 0; |
| 187 | if (const TemplateTypeParmType *TTP |
| 188 | = Unexpanded[I].first.dyn_cast<const TemplateTypeParmType *>()) |
Chandler Carruth | b7efff4 | 2011-05-01 01:05:51 +0000 | [diff] [blame] | 189 | Name = TTP->getIdentifier(); |
Douglas Gregor | 9ef7589 | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 190 | else |
| 191 | Name = Unexpanded[I].first.get<NamedDecl *>()->getIdentifier(); |
| 192 | |
| 193 | if (Name && NamesKnown.insert(Name)) |
| 194 | Names.push_back(Name); |
| 195 | |
| 196 | if (Unexpanded[I].second.isValid()) |
| 197 | Locations.push_back(Unexpanded[I].second); |
| 198 | } |
| 199 | |
| 200 | DiagnosticBuilder DB |
Douglas Gregor | 65019ac | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 201 | = Names.size() == 0? Diag(Loc, diag::err_unexpanded_parameter_pack_0) |
Douglas Gregor | 9ef7589 | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 202 | << (int)UPPC |
Douglas Gregor | 65019ac | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 203 | : Names.size() == 1? Diag(Loc, diag::err_unexpanded_parameter_pack_1) |
Douglas Gregor | 9ef7589 | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 204 | << (int)UPPC << Names[0] |
Douglas Gregor | 65019ac | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 205 | : Names.size() == 2? Diag(Loc, diag::err_unexpanded_parameter_pack_2) |
Douglas Gregor | 9ef7589 | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 206 | << (int)UPPC << Names[0] << Names[1] |
Douglas Gregor | 65019ac | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 207 | : Diag(Loc, diag::err_unexpanded_parameter_pack_3_or_more) |
Douglas Gregor | 9ef7589 | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 208 | << (int)UPPC << Names[0] << Names[1]; |
| 209 | |
| 210 | for (unsigned I = 0, N = Locations.size(); I != N; ++I) |
| 211 | DB << SourceRange(Locations[I]); |
| 212 | } |
| 213 | |
Douglas Gregor | c463335 | 2010-12-15 17:38:57 +0000 | [diff] [blame] | 214 | bool Sema::DiagnoseUnexpandedParameterPack(SourceLocation Loc, |
| 215 | TypeSourceInfo *T, |
| 216 | UnexpandedParameterPackContext UPPC) { |
| 217 | // C++0x [temp.variadic]p5: |
| 218 | // An appearance of a name of a parameter pack that is not expanded is |
| 219 | // ill-formed. |
| 220 | if (!T->getType()->containsUnexpandedParameterPack()) |
| 221 | return false; |
| 222 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 223 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
Douglas Gregor | 9ef7589 | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 224 | CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseTypeLoc( |
| 225 | T->getTypeLoc()); |
| 226 | assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs"); |
Douglas Gregor | 65019ac | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 227 | DiagnoseUnexpandedParameterPacks(Loc, UPPC, Unexpanded); |
Douglas Gregor | c463335 | 2010-12-15 17:38:57 +0000 | [diff] [blame] | 228 | return true; |
| 229 | } |
| 230 | |
| 231 | bool Sema::DiagnoseUnexpandedParameterPack(Expr *E, |
Douglas Gregor | 56c0458 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 232 | UnexpandedParameterPackContext UPPC) { |
Douglas Gregor | c463335 | 2010-12-15 17:38:57 +0000 | [diff] [blame] | 233 | // C++0x [temp.variadic]p5: |
| 234 | // An appearance of a name of a parameter pack that is not expanded is |
| 235 | // ill-formed. |
| 236 | if (!E->containsUnexpandedParameterPack()) |
| 237 | return false; |
| 238 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 239 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
Douglas Gregor | 9ef7589 | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 240 | CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseStmt(E); |
| 241 | assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs"); |
Douglas Gregor | 65019ac | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 242 | DiagnoseUnexpandedParameterPacks(E->getLocStart(), UPPC, Unexpanded); |
Douglas Gregor | c463335 | 2010-12-15 17:38:57 +0000 | [diff] [blame] | 243 | return true; |
| 244 | } |
Douglas Gregor | 56c0458 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 245 | |
| 246 | bool Sema::DiagnoseUnexpandedParameterPack(const CXXScopeSpec &SS, |
| 247 | UnexpandedParameterPackContext UPPC) { |
| 248 | // C++0x [temp.variadic]p5: |
| 249 | // An appearance of a name of a parameter pack that is not expanded is |
| 250 | // ill-formed. |
| 251 | if (!SS.getScopeRep() || |
| 252 | !SS.getScopeRep()->containsUnexpandedParameterPack()) |
| 253 | return false; |
| 254 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 255 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
Douglas Gregor | 56c0458 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 256 | CollectUnexpandedParameterPacksVisitor(Unexpanded) |
| 257 | .TraverseNestedNameSpecifier(SS.getScopeRep()); |
| 258 | assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs"); |
Douglas Gregor | 65019ac | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 259 | DiagnoseUnexpandedParameterPacks(SS.getRange().getBegin(), |
Douglas Gregor | 56c0458 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 260 | UPPC, Unexpanded); |
| 261 | return true; |
| 262 | } |
| 263 | |
| 264 | bool Sema::DiagnoseUnexpandedParameterPack(const DeclarationNameInfo &NameInfo, |
| 265 | UnexpandedParameterPackContext UPPC) { |
| 266 | // C++0x [temp.variadic]p5: |
| 267 | // An appearance of a name of a parameter pack that is not expanded is |
| 268 | // ill-formed. |
| 269 | switch (NameInfo.getName().getNameKind()) { |
| 270 | case DeclarationName::Identifier: |
| 271 | case DeclarationName::ObjCZeroArgSelector: |
| 272 | case DeclarationName::ObjCOneArgSelector: |
| 273 | case DeclarationName::ObjCMultiArgSelector: |
| 274 | case DeclarationName::CXXOperatorName: |
| 275 | case DeclarationName::CXXLiteralOperatorName: |
| 276 | case DeclarationName::CXXUsingDirective: |
| 277 | return false; |
| 278 | |
| 279 | case DeclarationName::CXXConstructorName: |
| 280 | case DeclarationName::CXXDestructorName: |
| 281 | case DeclarationName::CXXConversionFunctionName: |
Douglas Gregor | 099ffe8 | 2010-12-16 17:19:19 +0000 | [diff] [blame] | 282 | // FIXME: We shouldn't need this null check! |
Douglas Gregor | 0762bfd | 2010-12-16 01:40:04 +0000 | [diff] [blame] | 283 | if (TypeSourceInfo *TSInfo = NameInfo.getNamedTypeInfo()) |
| 284 | return DiagnoseUnexpandedParameterPack(NameInfo.getLoc(), TSInfo, UPPC); |
| 285 | |
| 286 | if (!NameInfo.getName().getCXXNameType()->containsUnexpandedParameterPack()) |
Douglas Gregor | 56c0458 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 287 | return false; |
Douglas Gregor | 0762bfd | 2010-12-16 01:40:04 +0000 | [diff] [blame] | 288 | |
Douglas Gregor | 56c0458 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 289 | break; |
| 290 | } |
| 291 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 292 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
Douglas Gregor | 56c0458 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 293 | CollectUnexpandedParameterPacksVisitor(Unexpanded) |
Douglas Gregor | 0762bfd | 2010-12-16 01:40:04 +0000 | [diff] [blame] | 294 | .TraverseType(NameInfo.getName().getCXXNameType()); |
Douglas Gregor | 56c0458 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 295 | assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs"); |
Douglas Gregor | 65019ac | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 296 | DiagnoseUnexpandedParameterPacks(NameInfo.getLoc(), UPPC, Unexpanded); |
Douglas Gregor | 56c0458 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 297 | return true; |
| 298 | } |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 299 | |
| 300 | bool Sema::DiagnoseUnexpandedParameterPack(SourceLocation Loc, |
| 301 | TemplateName Template, |
| 302 | UnexpandedParameterPackContext UPPC) { |
| 303 | |
| 304 | if (Template.isNull() || !Template.containsUnexpandedParameterPack()) |
| 305 | return false; |
| 306 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 307 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 308 | CollectUnexpandedParameterPacksVisitor(Unexpanded) |
| 309 | .TraverseTemplateName(Template); |
| 310 | assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs"); |
Douglas Gregor | 65019ac | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 311 | DiagnoseUnexpandedParameterPacks(Loc, UPPC, Unexpanded); |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 312 | return true; |
| 313 | } |
| 314 | |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 315 | bool Sema::DiagnoseUnexpandedParameterPack(TemplateArgumentLoc Arg, |
| 316 | UnexpandedParameterPackContext UPPC) { |
| 317 | if (Arg.getArgument().isNull() || |
| 318 | !Arg.getArgument().containsUnexpandedParameterPack()) |
| 319 | return false; |
| 320 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 321 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 322 | CollectUnexpandedParameterPacksVisitor(Unexpanded) |
| 323 | .TraverseTemplateArgumentLoc(Arg); |
| 324 | assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs"); |
Douglas Gregor | 65019ac | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 325 | DiagnoseUnexpandedParameterPacks(Arg.getLocation(), UPPC, Unexpanded); |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 326 | return true; |
| 327 | } |
| 328 | |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 329 | void Sema::collectUnexpandedParameterPacks(TemplateArgument Arg, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 330 | SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) { |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 331 | CollectUnexpandedParameterPacksVisitor(Unexpanded) |
| 332 | .TraverseTemplateArgument(Arg); |
| 333 | } |
| 334 | |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 335 | void Sema::collectUnexpandedParameterPacks(TemplateArgumentLoc Arg, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 336 | SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) { |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 337 | CollectUnexpandedParameterPacksVisitor(Unexpanded) |
| 338 | .TraverseTemplateArgumentLoc(Arg); |
| 339 | } |
| 340 | |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 341 | void Sema::collectUnexpandedParameterPacks(QualType T, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 342 | SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) { |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 343 | CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseType(T); |
| 344 | } |
| 345 | |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 346 | void Sema::collectUnexpandedParameterPacks(TypeLoc TL, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 347 | SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) { |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 348 | CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseTypeLoc(TL); |
| 349 | } |
| 350 | |
Douglas Gregor | 65019ac | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 351 | void Sema::collectUnexpandedParameterPacks(CXXScopeSpec &SS, |
| 352 | SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) { |
| 353 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
| 354 | if (!Qualifier) |
| 355 | return; |
| 356 | |
| 357 | NestedNameSpecifierLoc QualifierLoc(Qualifier, SS.location_data()); |
| 358 | CollectUnexpandedParameterPacksVisitor(Unexpanded) |
| 359 | .TraverseNestedNameSpecifierLoc(QualifierLoc); |
| 360 | } |
| 361 | |
| 362 | void Sema::collectUnexpandedParameterPacks(const DeclarationNameInfo &NameInfo, |
| 363 | SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) { |
| 364 | CollectUnexpandedParameterPacksVisitor(Unexpanded) |
| 365 | .TraverseDeclarationNameInfo(NameInfo); |
| 366 | } |
| 367 | |
| 368 | |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 369 | ParsedTemplateArgument |
| 370 | Sema::ActOnPackExpansion(const ParsedTemplateArgument &Arg, |
| 371 | SourceLocation EllipsisLoc) { |
| 372 | if (Arg.isInvalid()) |
| 373 | return Arg; |
| 374 | |
| 375 | switch (Arg.getKind()) { |
| 376 | case ParsedTemplateArgument::Type: { |
| 377 | TypeResult Result = ActOnPackExpansion(Arg.getAsType(), EllipsisLoc); |
| 378 | if (Result.isInvalid()) |
| 379 | return ParsedTemplateArgument(); |
| 380 | |
| 381 | return ParsedTemplateArgument(Arg.getKind(), Result.get().getAsOpaquePtr(), |
| 382 | Arg.getLocation()); |
| 383 | } |
| 384 | |
Douglas Gregor | be230c3 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 385 | case ParsedTemplateArgument::NonType: { |
| 386 | ExprResult Result = ActOnPackExpansion(Arg.getAsExpr(), EllipsisLoc); |
| 387 | if (Result.isInvalid()) |
| 388 | return ParsedTemplateArgument(); |
| 389 | |
| 390 | return ParsedTemplateArgument(Arg.getKind(), Result.get(), |
| 391 | Arg.getLocation()); |
| 392 | } |
| 393 | |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 394 | case ParsedTemplateArgument::Template: |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 395 | if (!Arg.getAsTemplate().get().containsUnexpandedParameterPack()) { |
| 396 | SourceRange R(Arg.getLocation()); |
| 397 | if (Arg.getScopeSpec().isValid()) |
| 398 | R.setBegin(Arg.getScopeSpec().getBeginLoc()); |
| 399 | Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
| 400 | << R; |
| 401 | return ParsedTemplateArgument(); |
| 402 | } |
| 403 | |
| 404 | return Arg.getTemplatePackExpansion(EllipsisLoc); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 405 | } |
| 406 | llvm_unreachable("Unhandled template argument kind?"); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | TypeResult Sema::ActOnPackExpansion(ParsedType Type, |
| 410 | SourceLocation EllipsisLoc) { |
| 411 | TypeSourceInfo *TSInfo; |
| 412 | GetTypeFromParser(Type, &TSInfo); |
| 413 | if (!TSInfo) |
| 414 | return true; |
| 415 | |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 416 | TypeSourceInfo *TSResult = CheckPackExpansion(TSInfo, EllipsisLoc, |
| 417 | llvm::Optional<unsigned>()); |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 418 | if (!TSResult) |
| 419 | return true; |
| 420 | |
| 421 | return CreateParsedType(TSResult->getType(), TSResult); |
| 422 | } |
| 423 | |
| 424 | TypeSourceInfo *Sema::CheckPackExpansion(TypeSourceInfo *Pattern, |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 425 | SourceLocation EllipsisLoc, |
| 426 | llvm::Optional<unsigned> NumExpansions) { |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 427 | // Create the pack expansion type and source-location information. |
Douglas Gregor | 2fc1bb7 | 2011-01-12 17:07:58 +0000 | [diff] [blame] | 428 | QualType Result = CheckPackExpansion(Pattern->getType(), |
| 429 | Pattern->getTypeLoc().getSourceRange(), |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 430 | EllipsisLoc, NumExpansions); |
Douglas Gregor | 2fc1bb7 | 2011-01-12 17:07:58 +0000 | [diff] [blame] | 431 | if (Result.isNull()) |
| 432 | return 0; |
| 433 | |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 434 | TypeSourceInfo *TSResult = Context.CreateTypeSourceInfo(Result); |
| 435 | PackExpansionTypeLoc TL = cast<PackExpansionTypeLoc>(TSResult->getTypeLoc()); |
| 436 | TL.setEllipsisLoc(EllipsisLoc); |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 437 | |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 438 | // Copy over the source-location information from the type. |
| 439 | memcpy(TL.getNextTypeLoc().getOpaqueData(), |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 440 | Pattern->getTypeLoc().getOpaqueData(), |
| 441 | Pattern->getTypeLoc().getFullDataSize()); |
| 442 | return TSResult; |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 443 | } |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 444 | |
Douglas Gregor | 2fc1bb7 | 2011-01-12 17:07:58 +0000 | [diff] [blame] | 445 | QualType Sema::CheckPackExpansion(QualType Pattern, |
| 446 | SourceRange PatternRange, |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 447 | SourceLocation EllipsisLoc, |
| 448 | llvm::Optional<unsigned> NumExpansions) { |
Douglas Gregor | 2fc1bb7 | 2011-01-12 17:07:58 +0000 | [diff] [blame] | 449 | // C++0x [temp.variadic]p5: |
| 450 | // The pattern of a pack expansion shall name one or more |
| 451 | // parameter packs that are not expanded by a nested pack |
| 452 | // expansion. |
| 453 | if (!Pattern->containsUnexpandedParameterPack()) { |
| 454 | Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
| 455 | << PatternRange; |
| 456 | return QualType(); |
| 457 | } |
| 458 | |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 459 | return Context.getPackExpansionType(Pattern, NumExpansions); |
Douglas Gregor | 2fc1bb7 | 2011-01-12 17:07:58 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Douglas Gregor | be230c3 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 462 | ExprResult Sema::ActOnPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc) { |
Douglas Gregor | 67fd125 | 2011-01-14 21:20:45 +0000 | [diff] [blame] | 463 | return CheckPackExpansion(Pattern, EllipsisLoc, llvm::Optional<unsigned>()); |
| 464 | } |
| 465 | |
| 466 | ExprResult Sema::CheckPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc, |
| 467 | llvm::Optional<unsigned> NumExpansions) { |
Douglas Gregor | be230c3 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 468 | if (!Pattern) |
| 469 | return ExprError(); |
| 470 | |
| 471 | // C++0x [temp.variadic]p5: |
| 472 | // The pattern of a pack expansion shall name one or more |
| 473 | // parameter packs that are not expanded by a nested pack |
| 474 | // expansion. |
| 475 | if (!Pattern->containsUnexpandedParameterPack()) { |
| 476 | Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
| 477 | << Pattern->getSourceRange(); |
| 478 | return ExprError(); |
| 479 | } |
| 480 | |
| 481 | // Create the pack expansion expression and source-location information. |
| 482 | return Owned(new (Context) PackExpansionExpr(Context.DependentTy, Pattern, |
Douglas Gregor | 67fd125 | 2011-01-14 21:20:45 +0000 | [diff] [blame] | 483 | EllipsisLoc, NumExpansions)); |
Douglas Gregor | be230c3 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 484 | } |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 485 | |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 486 | /// \brief Retrieve the depth and index of a parameter pack. |
| 487 | static std::pair<unsigned, unsigned> |
| 488 | getDepthAndIndex(NamedDecl *ND) { |
| 489 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND)) |
| 490 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
| 491 | |
| 492 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND)) |
| 493 | return std::make_pair(NTTP->getDepth(), NTTP->getIndex()); |
| 494 | |
| 495 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND); |
| 496 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
| 497 | } |
| 498 | |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 499 | bool Sema::CheckParameterPacksForExpansion(SourceLocation EllipsisLoc, |
| 500 | SourceRange PatternRange, |
David Blaikie | a71f9d0 | 2011-09-22 02:34:54 +0000 | [diff] [blame] | 501 | ArrayRef<UnexpandedParameterPack> Unexpanded, |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 502 | const MultiLevelTemplateArgumentList &TemplateArgs, |
| 503 | bool &ShouldExpand, |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 504 | bool &RetainExpansion, |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 505 | llvm::Optional<unsigned> &NumExpansions) { |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 506 | ShouldExpand = true; |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 507 | RetainExpansion = false; |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 508 | std::pair<IdentifierInfo *, SourceLocation> FirstPack; |
| 509 | bool HaveFirstPack = false; |
| 510 | |
David Blaikie | a71f9d0 | 2011-09-22 02:34:54 +0000 | [diff] [blame] | 511 | for (ArrayRef<UnexpandedParameterPack>::iterator i = Unexpanded.begin(), |
| 512 | end = Unexpanded.end(); |
| 513 | i != end; ++i) { |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 514 | // Compute the depth and index for this parameter pack. |
Ted Kremenek | 9577abc | 2011-01-23 17:04:59 +0000 | [diff] [blame] | 515 | unsigned Depth = 0, Index = 0; |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 516 | IdentifierInfo *Name; |
Douglas Gregor | 12c9c00 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 517 | bool IsFunctionParameterPack = false; |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 518 | |
| 519 | if (const TemplateTypeParmType *TTP |
David Blaikie | a71f9d0 | 2011-09-22 02:34:54 +0000 | [diff] [blame] | 520 | = i->first.dyn_cast<const TemplateTypeParmType *>()) { |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 521 | Depth = TTP->getDepth(); |
| 522 | Index = TTP->getIndex(); |
Chandler Carruth | b7efff4 | 2011-05-01 01:05:51 +0000 | [diff] [blame] | 523 | Name = TTP->getIdentifier(); |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 524 | } else { |
David Blaikie | a71f9d0 | 2011-09-22 02:34:54 +0000 | [diff] [blame] | 525 | NamedDecl *ND = i->first.get<NamedDecl *>(); |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 526 | if (isa<ParmVarDecl>(ND)) |
Douglas Gregor | 12c9c00 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 527 | IsFunctionParameterPack = true; |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 528 | else |
| 529 | llvm::tie(Depth, Index) = getDepthAndIndex(ND); |
| 530 | |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 531 | Name = ND->getIdentifier(); |
| 532 | } |
| 533 | |
Douglas Gregor | 12c9c00 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 534 | // Determine the size of this argument pack. |
| 535 | unsigned NewPackSize; |
| 536 | if (IsFunctionParameterPack) { |
| 537 | // Figure out whether we're instantiating to an argument pack or not. |
| 538 | typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack; |
| 539 | |
| 540 | llvm::PointerUnion<Decl *, DeclArgumentPack *> *Instantiation |
| 541 | = CurrentInstantiationScope->findInstantiationOf( |
David Blaikie | a71f9d0 | 2011-09-22 02:34:54 +0000 | [diff] [blame] | 542 | i->first.get<NamedDecl *>()); |
Chris Lattner | a70062f | 2011-02-17 19:38:27 +0000 | [diff] [blame] | 543 | if (Instantiation->is<DeclArgumentPack *>()) { |
Douglas Gregor | 12c9c00 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 544 | // We could expand this function parameter pack. |
| 545 | NewPackSize = Instantiation->get<DeclArgumentPack *>()->size(); |
| 546 | } else { |
| 547 | // We can't expand this function parameter pack, so we can't expand |
| 548 | // the pack expansion. |
| 549 | ShouldExpand = false; |
| 550 | continue; |
| 551 | } |
| 552 | } else { |
| 553 | // If we don't have a template argument at this depth/index, then we |
| 554 | // cannot expand the pack expansion. Make a note of this, but we still |
| 555 | // want to check any parameter packs we *do* have arguments for. |
| 556 | if (Depth >= TemplateArgs.getNumLevels() || |
| 557 | !TemplateArgs.hasTemplateArgument(Depth, Index)) { |
| 558 | ShouldExpand = false; |
| 559 | continue; |
| 560 | } |
| 561 | |
| 562 | // Determine the size of the argument pack. |
| 563 | NewPackSize = TemplateArgs(Depth, Index).pack_size(); |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 566 | // C++0x [temp.arg.explicit]p9: |
| 567 | // Template argument deduction can extend the sequence of template |
| 568 | // arguments corresponding to a template parameter pack, even when the |
| 569 | // sequence contains explicitly specified template arguments. |
Douglas Gregor | 8619edd | 2011-01-20 23:15:49 +0000 | [diff] [blame] | 570 | if (!IsFunctionParameterPack) { |
| 571 | if (NamedDecl *PartialPack |
| 572 | = CurrentInstantiationScope->getPartiallySubstitutedPack()){ |
| 573 | unsigned PartialDepth, PartialIndex; |
| 574 | llvm::tie(PartialDepth, PartialIndex) = getDepthAndIndex(PartialPack); |
| 575 | if (PartialDepth == Depth && PartialIndex == Index) |
| 576 | RetainExpansion = true; |
| 577 | } |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 578 | } |
Douglas Gregor | 8619edd | 2011-01-20 23:15:49 +0000 | [diff] [blame] | 579 | |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 580 | if (!NumExpansions) { |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 581 | // The is the first pack we've seen for which we have an argument. |
| 582 | // Record it. |
| 583 | NumExpansions = NewPackSize; |
| 584 | FirstPack.first = Name; |
David Blaikie | a71f9d0 | 2011-09-22 02:34:54 +0000 | [diff] [blame] | 585 | FirstPack.second = i->second; |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 586 | HaveFirstPack = true; |
| 587 | continue; |
| 588 | } |
| 589 | |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 590 | if (NewPackSize != *NumExpansions) { |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 591 | // C++0x [temp.variadic]p5: |
| 592 | // All of the parameter packs expanded by a pack expansion shall have |
| 593 | // the same number of arguments specified. |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 594 | if (HaveFirstPack) |
| 595 | Diag(EllipsisLoc, diag::err_pack_expansion_length_conflict) |
| 596 | << FirstPack.first << Name << *NumExpansions << NewPackSize |
David Blaikie | a71f9d0 | 2011-09-22 02:34:54 +0000 | [diff] [blame] | 597 | << SourceRange(FirstPack.second) << SourceRange(i->second); |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 598 | else |
| 599 | Diag(EllipsisLoc, diag::err_pack_expansion_length_conflict_multilevel) |
| 600 | << Name << *NumExpansions << NewPackSize |
David Blaikie | a71f9d0 | 2011-09-22 02:34:54 +0000 | [diff] [blame] | 601 | << SourceRange(i->second); |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 602 | return true; |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | return false; |
| 607 | } |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 608 | |
Douglas Gregor | 21371ea | 2011-01-11 03:14:20 +0000 | [diff] [blame] | 609 | unsigned Sema::getNumArgumentsInExpansion(QualType T, |
| 610 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 611 | QualType Pattern = cast<PackExpansionType>(T)->getPattern(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 612 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
Douglas Gregor | 21371ea | 2011-01-11 03:14:20 +0000 | [diff] [blame] | 613 | CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseType(Pattern); |
| 614 | |
| 615 | for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) { |
| 616 | // Compute the depth and index for this parameter pack. |
| 617 | unsigned Depth; |
| 618 | unsigned Index; |
| 619 | |
| 620 | if (const TemplateTypeParmType *TTP |
| 621 | = Unexpanded[I].first.dyn_cast<const TemplateTypeParmType *>()) { |
| 622 | Depth = TTP->getDepth(); |
| 623 | Index = TTP->getIndex(); |
| 624 | } else { |
| 625 | NamedDecl *ND = Unexpanded[I].first.get<NamedDecl *>(); |
| 626 | if (isa<ParmVarDecl>(ND)) { |
| 627 | // Function parameter pack. |
| 628 | typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack; |
| 629 | |
| 630 | llvm::PointerUnion<Decl *, DeclArgumentPack *> *Instantiation |
| 631 | = CurrentInstantiationScope->findInstantiationOf( |
| 632 | Unexpanded[I].first.get<NamedDecl *>()); |
Chris Lattner | a70062f | 2011-02-17 19:38:27 +0000 | [diff] [blame] | 633 | if (Instantiation->is<DeclArgumentPack *>()) |
Douglas Gregor | 21371ea | 2011-01-11 03:14:20 +0000 | [diff] [blame] | 634 | return Instantiation->get<DeclArgumentPack *>()->size(); |
| 635 | |
| 636 | continue; |
| 637 | } |
| 638 | |
| 639 | llvm::tie(Depth, Index) = getDepthAndIndex(ND); |
| 640 | } |
| 641 | if (Depth >= TemplateArgs.getNumLevels() || |
| 642 | !TemplateArgs.hasTemplateArgument(Depth, Index)) |
| 643 | continue; |
| 644 | |
| 645 | // Determine the size of the argument pack. |
| 646 | return TemplateArgs(Depth, Index).pack_size(); |
| 647 | } |
| 648 | |
| 649 | llvm_unreachable("No unexpanded parameter packs in type expansion."); |
Douglas Gregor | 21371ea | 2011-01-11 03:14:20 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 652 | bool Sema::containsUnexpandedParameterPacks(Declarator &D) { |
| 653 | const DeclSpec &DS = D.getDeclSpec(); |
| 654 | switch (DS.getTypeSpecType()) { |
| 655 | case TST_typename: |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 656 | case TST_typeofType: |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 657 | case TST_underlyingType: |
| 658 | case TST_atomic: { |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 659 | QualType T = DS.getRepAsType().get(); |
| 660 | if (!T.isNull() && T->containsUnexpandedParameterPack()) |
| 661 | return true; |
| 662 | break; |
| 663 | } |
| 664 | |
| 665 | case TST_typeofExpr: |
| 666 | case TST_decltype: |
| 667 | if (DS.getRepAsExpr() && |
| 668 | DS.getRepAsExpr()->containsUnexpandedParameterPack()) |
| 669 | return true; |
| 670 | break; |
| 671 | |
| 672 | case TST_unspecified: |
| 673 | case TST_void: |
| 674 | case TST_char: |
| 675 | case TST_wchar: |
| 676 | case TST_char16: |
| 677 | case TST_char32: |
| 678 | case TST_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 679 | case TST_half: |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 680 | case TST_float: |
| 681 | case TST_double: |
| 682 | case TST_bool: |
| 683 | case TST_decimal32: |
| 684 | case TST_decimal64: |
| 685 | case TST_decimal128: |
| 686 | case TST_enum: |
| 687 | case TST_union: |
| 688 | case TST_struct: |
| 689 | case TST_class: |
| 690 | case TST_auto: |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 691 | case TST_unknown_anytype: |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 692 | case TST_error: |
| 693 | break; |
| 694 | } |
| 695 | |
| 696 | for (unsigned I = 0, N = D.getNumTypeObjects(); I != N; ++I) { |
| 697 | const DeclaratorChunk &Chunk = D.getTypeObject(I); |
| 698 | switch (Chunk.Kind) { |
| 699 | case DeclaratorChunk::Pointer: |
| 700 | case DeclaratorChunk::Reference: |
| 701 | case DeclaratorChunk::Paren: |
| 702 | // These declarator chunks cannot contain any parameter packs. |
| 703 | break; |
| 704 | |
| 705 | case DeclaratorChunk::Array: |
| 706 | case DeclaratorChunk::Function: |
| 707 | case DeclaratorChunk::BlockPointer: |
| 708 | // Syntactically, these kinds of declarator chunks all come after the |
| 709 | // declarator-id (conceptually), so the parser should not invoke this |
| 710 | // routine at this time. |
| 711 | llvm_unreachable("Could not have seen this kind of declarator chunk"); |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 712 | |
| 713 | case DeclaratorChunk::MemberPointer: |
| 714 | if (Chunk.Mem.Scope().getScopeRep() && |
| 715 | Chunk.Mem.Scope().getScopeRep()->containsUnexpandedParameterPack()) |
| 716 | return true; |
| 717 | break; |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | return false; |
| 722 | } |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 723 | |
Kaelyn Uhrain | f8ec8c9 | 2012-01-13 23:10:36 +0000 | [diff] [blame] | 724 | namespace { |
| 725 | |
| 726 | // Callback to only accept typo corrections that refer to parameter packs. |
| 727 | class ParameterPackValidatorCCC : public CorrectionCandidateCallback { |
| 728 | public: |
| 729 | virtual bool ValidateCandidate(const TypoCorrection &candidate) { |
| 730 | NamedDecl *ND = candidate.getCorrectionDecl(); |
| 731 | return ND && ND->isParameterPack(); |
| 732 | } |
| 733 | }; |
| 734 | |
| 735 | } |
| 736 | |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 737 | /// \brief Called when an expression computing the size of a parameter pack |
| 738 | /// is parsed. |
| 739 | /// |
| 740 | /// \code |
| 741 | /// template<typename ...Types> struct count { |
| 742 | /// static const unsigned value = sizeof...(Types); |
| 743 | /// }; |
| 744 | /// \endcode |
| 745 | /// |
| 746 | // |
| 747 | /// \param OpLoc The location of the "sizeof" keyword. |
| 748 | /// \param Name The name of the parameter pack whose size will be determined. |
| 749 | /// \param NameLoc The source location of the name of the parameter pack. |
| 750 | /// \param RParenLoc The location of the closing parentheses. |
| 751 | ExprResult Sema::ActOnSizeofParameterPackExpr(Scope *S, |
| 752 | SourceLocation OpLoc, |
| 753 | IdentifierInfo &Name, |
| 754 | SourceLocation NameLoc, |
| 755 | SourceLocation RParenLoc) { |
| 756 | // C++0x [expr.sizeof]p5: |
| 757 | // The identifier in a sizeof... expression shall name a parameter pack. |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 758 | LookupResult R(*this, &Name, NameLoc, LookupOrdinaryName); |
| 759 | LookupName(R, S); |
| 760 | |
| 761 | NamedDecl *ParameterPack = 0; |
Kaelyn Uhrain | f8ec8c9 | 2012-01-13 23:10:36 +0000 | [diff] [blame] | 762 | ParameterPackValidatorCCC Validator; |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 763 | switch (R.getResultKind()) { |
| 764 | case LookupResult::Found: |
| 765 | ParameterPack = R.getFoundDecl(); |
| 766 | break; |
| 767 | |
| 768 | case LookupResult::NotFound: |
| 769 | case LookupResult::NotFoundInCurrentInstantiation: |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 770 | if (TypoCorrection Corrected = CorrectTypo(R.getLookupNameInfo(), |
Kaelyn Uhrain | f8ec8c9 | 2012-01-13 23:10:36 +0000 | [diff] [blame] | 771 | R.getLookupKind(), S, 0, |
Kaelyn Uhrain | 16e46dd | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 772 | Validator)) { |
Kaelyn Uhrain | f8ec8c9 | 2012-01-13 23:10:36 +0000 | [diff] [blame] | 773 | std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOptions())); |
| 774 | ParameterPack = Corrected.getCorrectionDecl(); |
| 775 | Diag(NameLoc, diag::err_sizeof_pack_no_pack_name_suggest) |
| 776 | << &Name << CorrectedQuotedStr |
| 777 | << FixItHint::CreateReplacement( |
| 778 | NameLoc, Corrected.getAsString(getLangOptions())); |
| 779 | Diag(ParameterPack->getLocation(), diag::note_parameter_pack_here) |
| 780 | << CorrectedQuotedStr; |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | case LookupResult::FoundOverloaded: |
| 784 | case LookupResult::FoundUnresolvedValue: |
| 785 | break; |
| 786 | |
| 787 | case LookupResult::Ambiguous: |
| 788 | DiagnoseAmbiguousLookup(R); |
| 789 | return ExprError(); |
| 790 | } |
| 791 | |
Douglas Gregor | 1fe85ea | 2011-01-05 21:11:38 +0000 | [diff] [blame] | 792 | if (!ParameterPack || !ParameterPack->isParameterPack()) { |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 793 | Diag(NameLoc, diag::err_sizeof_pack_no_pack_name) |
| 794 | << &Name; |
| 795 | return ExprError(); |
| 796 | } |
| 797 | |
Eli Friedman | 88530d5 | 2012-03-01 21:32:56 +0000 | [diff] [blame] | 798 | MarkAnyDeclReferenced(OpLoc, ParameterPack); |
| 799 | |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 800 | return new (Context) SizeOfPackExpr(Context.getSizeType(), OpLoc, |
| 801 | ParameterPack, NameLoc, RParenLoc); |
| 802 | } |