Douglas Gregor | b55fdf8 | 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" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 13 | #include "TypeLocBuilder.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 14 | #include "clang/AST/Expr.h" |
| 15 | #include "clang/AST/RecursiveASTVisitor.h" |
| 16 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | 820ba7b | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 17 | #include "clang/Sema/Lookup.h" |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 18 | #include "clang/Sema/ParsedTemplate.h" |
Richard Smith | 2589b980 | 2012-07-25 03:56:55 +0000 | [diff] [blame] | 19 | #include "clang/Sema/ScopeInfo.h" |
Douglas Gregor | b55fdf8 | 2010-12-15 17:38:57 +0000 | [diff] [blame] | 20 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | 840bd6c | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 21 | #include "clang/Sema/Template.h" |
Douglas Gregor | b55fdf8 | 2010-12-15 17:38:57 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace clang; |
| 24 | |
Douglas Gregor | 1da294a | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 25 | //---------------------------------------------------------------------------- |
| 26 | // Visitor that collects unexpanded parameter packs |
| 27 | //---------------------------------------------------------------------------- |
| 28 | |
Douglas Gregor | 1da294a | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 37 | SmallVectorImpl<UnexpandedParameterPack> &Unexpanded; |
Douglas Gregor | 1da294a | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 38 | |
Richard Smith | 2589b980 | 2012-07-25 03:56:55 +0000 | [diff] [blame] | 39 | bool InLambda; |
| 40 | |
Douglas Gregor | 1da294a | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 41 | public: |
| 42 | explicit CollectUnexpandedParameterPacksVisitor( |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 43 | SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) |
Richard Smith | 2589b980 | 2012-07-25 03:56:55 +0000 | [diff] [blame] | 44 | : Unexpanded(Unexpanded), InLambda(false) { } |
Douglas Gregor | 1da294a | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 45 | |
Douglas Gregor | 15b4ec2 | 2010-12-20 23:07:20 +0000 | [diff] [blame] | 46 | bool shouldWalkTypesOfTypeLocs() const { return false; } |
| 47 | |
Douglas Gregor | 1da294a | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 48 | //------------------------------------------------------------------------ |
| 49 | // Recording occurrences of (unexpanded) parameter packs. |
| 50 | //------------------------------------------------------------------------ |
| 51 | |
| 52 | /// \brief Record occurrences of template type parameter packs. |
| 53 | bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
| 54 | if (TL.getTypePtr()->isParameterPack()) |
| 55 | Unexpanded.push_back(std::make_pair(TL.getTypePtr(), TL.getNameLoc())); |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | /// \brief Record occurrences of template type parameter packs |
| 60 | /// when we don't have proper source-location information for |
| 61 | /// them. |
| 62 | /// |
| 63 | /// Ideally, this routine would never be used. |
| 64 | bool VisitTemplateTypeParmType(TemplateTypeParmType *T) { |
| 65 | if (T->isParameterPack()) |
| 66 | Unexpanded.push_back(std::make_pair(T, SourceLocation())); |
| 67 | |
| 68 | return true; |
| 69 | } |
| 70 | |
Douglas Gregor | 476e302 | 2011-01-19 21:32:01 +0000 | [diff] [blame] | 71 | /// \brief Record occurrences of function and non-type template |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 72 | /// parameter packs in an expression. |
| 73 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 74 | if (E->getDecl()->isParameterPack()) |
| 75 | Unexpanded.push_back(std::make_pair(E->getDecl(), E->getLocation())); |
Douglas Gregor | da3cc0d | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 76 | |
| 77 | return true; |
| 78 | } |
| 79 | |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 80 | /// \brief Record occurrences of template template parameter packs. |
| 81 | bool TraverseTemplateName(TemplateName Template) { |
| 82 | if (TemplateTemplateParmDecl *TTP |
| 83 | = dyn_cast_or_null<TemplateTemplateParmDecl>( |
| 84 | Template.getAsTemplateDecl())) |
| 85 | if (TTP->isParameterPack()) |
| 86 | Unexpanded.push_back(std::make_pair(TTP, SourceLocation())); |
| 87 | |
| 88 | return inherited::TraverseTemplateName(Template); |
| 89 | } |
Douglas Gregor | 1da294a | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 90 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 91 | /// \brief Suppress traversal into Objective-C container literal |
| 92 | /// elements that are pack expansions. |
| 93 | bool TraverseObjCDictionaryLiteral(ObjCDictionaryLiteral *E) { |
| 94 | if (!E->containsUnexpandedParameterPack()) |
| 95 | return true; |
| 96 | |
| 97 | for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) { |
| 98 | ObjCDictionaryElement Element = E->getKeyValueElement(I); |
| 99 | if (Element.isPackExpansion()) |
| 100 | continue; |
| 101 | |
| 102 | TraverseStmt(Element.Key); |
| 103 | TraverseStmt(Element.Value); |
| 104 | } |
| 105 | return true; |
| 106 | } |
Douglas Gregor | 1da294a | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 107 | //------------------------------------------------------------------------ |
| 108 | // Pruning the search for unexpanded parameter packs. |
| 109 | //------------------------------------------------------------------------ |
| 110 | |
| 111 | /// \brief Suppress traversal into statements and expressions that |
| 112 | /// do not contain unexpanded parameter packs. |
| 113 | bool TraverseStmt(Stmt *S) { |
Richard Smith | 2589b980 | 2012-07-25 03:56:55 +0000 | [diff] [blame] | 114 | Expr *E = dyn_cast_or_null<Expr>(S); |
| 115 | if ((E && E->containsUnexpandedParameterPack()) || InLambda) |
| 116 | return inherited::TraverseStmt(S); |
Douglas Gregor | 1da294a | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 117 | |
Richard Smith | 2589b980 | 2012-07-25 03:56:55 +0000 | [diff] [blame] | 118 | return true; |
Douglas Gregor | 1da294a | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /// \brief Suppress traversal into types that do not contain |
| 122 | /// unexpanded parameter packs. |
| 123 | bool TraverseType(QualType T) { |
Richard Smith | 2589b980 | 2012-07-25 03:56:55 +0000 | [diff] [blame] | 124 | if ((!T.isNull() && T->containsUnexpandedParameterPack()) || InLambda) |
Douglas Gregor | 1da294a | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 125 | return inherited::TraverseType(T); |
| 126 | |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | /// \brief Suppress traversel into types with location information |
| 131 | /// that do not contain unexpanded parameter packs. |
| 132 | bool TraverseTypeLoc(TypeLoc TL) { |
Richard Smith | 2589b980 | 2012-07-25 03:56:55 +0000 | [diff] [blame] | 133 | if ((!TL.getType().isNull() && |
| 134 | TL.getType()->containsUnexpandedParameterPack()) || |
| 135 | InLambda) |
Douglas Gregor | 1da294a | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 136 | return inherited::TraverseTypeLoc(TL); |
| 137 | |
| 138 | return true; |
| 139 | } |
| 140 | |
Douglas Gregor | a8461bb | 2010-12-15 21:57:59 +0000 | [diff] [blame] | 141 | /// \brief Suppress traversal of non-parameter declarations, since |
| 142 | /// they cannot contain unexpanded parameter packs. |
| 143 | bool TraverseDecl(Decl *D) { |
Richard Smith | 2589b980 | 2012-07-25 03:56:55 +0000 | [diff] [blame] | 144 | if ((D && isa<ParmVarDecl>(D)) || InLambda) |
Douglas Gregor | a8461bb | 2010-12-15 21:57:59 +0000 | [diff] [blame] | 145 | return inherited::TraverseDecl(D); |
| 146 | |
Richard Smith | 2589b980 | 2012-07-25 03:56:55 +0000 | [diff] [blame] | 147 | return true; |
Douglas Gregor | a8461bb | 2010-12-15 21:57:59 +0000 | [diff] [blame] | 148 | } |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 149 | |
| 150 | /// \brief Suppress traversal of template argument pack expansions. |
| 151 | bool TraverseTemplateArgument(const TemplateArgument &Arg) { |
| 152 | if (Arg.isPackExpansion()) |
| 153 | return true; |
| 154 | |
| 155 | return inherited::TraverseTemplateArgument(Arg); |
| 156 | } |
| 157 | |
| 158 | /// \brief Suppress traversal of template argument pack expansions. |
| 159 | bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) { |
| 160 | if (ArgLoc.getArgument().isPackExpansion()) |
| 161 | return true; |
| 162 | |
| 163 | return inherited::TraverseTemplateArgumentLoc(ArgLoc); |
| 164 | } |
Richard Smith | 2589b980 | 2012-07-25 03:56:55 +0000 | [diff] [blame] | 165 | |
| 166 | /// \brief Note whether we're traversing a lambda containing an unexpanded |
| 167 | /// parameter pack. In this case, the unexpanded pack can occur anywhere, |
| 168 | /// including all the places where we normally wouldn't look. Within a |
| 169 | /// lambda, we don't propagate the 'contains unexpanded parameter pack' bit |
| 170 | /// outside an expression. |
| 171 | bool TraverseLambdaExpr(LambdaExpr *Lambda) { |
| 172 | // The ContainsUnexpandedParameterPack bit on a lambda is always correct, |
| 173 | // even if it's contained within another lambda. |
| 174 | if (!Lambda->containsUnexpandedParameterPack()) |
| 175 | return true; |
| 176 | |
| 177 | bool WasInLambda = InLambda; |
| 178 | InLambda = true; |
| 179 | |
| 180 | // If any capture names a function parameter pack, that pack is expanded |
| 181 | // when the lambda is expanded. |
| 182 | for (LambdaExpr::capture_iterator I = Lambda->capture_begin(), |
Richard Smith | ba71c08 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 183 | E = Lambda->capture_end(); |
| 184 | I != E; ++I) { |
| 185 | if (I->capturesVariable()) { |
| 186 | VarDecl *VD = I->getCapturedVar(); |
Richard Smith | 2589b980 | 2012-07-25 03:56:55 +0000 | [diff] [blame] | 187 | if (VD->isParameterPack()) |
| 188 | Unexpanded.push_back(std::make_pair(VD, I->getLocation())); |
Richard Smith | ba71c08 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
Richard Smith | 2589b980 | 2012-07-25 03:56:55 +0000 | [diff] [blame] | 191 | |
| 192 | inherited::TraverseLambdaExpr(Lambda); |
| 193 | |
| 194 | InLambda = WasInLambda; |
| 195 | return true; |
| 196 | } |
Douglas Gregor | 1da294a | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 197 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 198 | } |
Douglas Gregor | 1da294a | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 199 | |
Richard Smith | 36ee9fb | 2014-08-11 23:30:23 +0000 | [diff] [blame] | 200 | /// \brief Determine whether it's possible for an unexpanded parameter pack to |
| 201 | /// be valid in this location. This only happens when we're in a declaration |
| 202 | /// that is nested within an expression that could be expanded, such as a |
| 203 | /// lambda-expression within a function call. |
| 204 | /// |
| 205 | /// This is conservatively correct, but may claim that some unexpanded packs are |
| 206 | /// permitted when they are not. |
| 207 | bool Sema::isUnexpandedParameterPackPermitted() { |
| 208 | for (auto *SI : FunctionScopes) |
| 209 | if (isa<sema::LambdaScopeInfo>(SI)) |
| 210 | return true; |
| 211 | return false; |
| 212 | } |
| 213 | |
Douglas Gregor | 1da294a | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 214 | /// \brief Diagnose all of the unexpanded parameter packs in the given |
| 215 | /// vector. |
Richard Smith | 2589b980 | 2012-07-25 03:56:55 +0000 | [diff] [blame] | 216 | bool |
Douglas Gregor | 4a2a8f7 | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 217 | Sema::DiagnoseUnexpandedParameterPacks(SourceLocation Loc, |
| 218 | UnexpandedParameterPackContext UPPC, |
Bill Wendling | 8ac06af | 2012-02-22 09:38:11 +0000 | [diff] [blame] | 219 | ArrayRef<UnexpandedParameterPack> Unexpanded) { |
Douglas Gregor | 4a2a8f7 | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 220 | if (Unexpanded.empty()) |
Richard Smith | 2589b980 | 2012-07-25 03:56:55 +0000 | [diff] [blame] | 221 | return false; |
| 222 | |
| 223 | // If we are within a lambda expression, that lambda contains an unexpanded |
| 224 | // parameter pack, and we are done. |
| 225 | // FIXME: Store 'Unexpanded' on the lambda so we don't need to recompute it |
| 226 | // later. |
| 227 | for (unsigned N = FunctionScopes.size(); N; --N) { |
| 228 | if (sema::LambdaScopeInfo *LSI = |
| 229 | dyn_cast<sema::LambdaScopeInfo>(FunctionScopes[N-1])) { |
| 230 | LSI->ContainsUnexpandedParameterPack = true; |
| 231 | return false; |
| 232 | } |
| 233 | } |
Douglas Gregor | 4a2a8f7 | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 234 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 235 | SmallVector<SourceLocation, 4> Locations; |
| 236 | SmallVector<IdentifierInfo *, 4> Names; |
Douglas Gregor | 1da294a | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 237 | llvm::SmallPtrSet<IdentifierInfo *, 4> NamesKnown; |
| 238 | |
| 239 | for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 240 | IdentifierInfo *Name = nullptr; |
Douglas Gregor | 1da294a | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 241 | if (const TemplateTypeParmType *TTP |
| 242 | = Unexpanded[I].first.dyn_cast<const TemplateTypeParmType *>()) |
Chandler Carruth | dde65ea | 2011-05-01 01:05:51 +0000 | [diff] [blame] | 243 | Name = TTP->getIdentifier(); |
Douglas Gregor | 1da294a | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 244 | else |
| 245 | Name = Unexpanded[I].first.get<NamedDecl *>()->getIdentifier(); |
| 246 | |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 247 | if (Name && NamesKnown.insert(Name).second) |
Douglas Gregor | 1da294a | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 248 | Names.push_back(Name); |
| 249 | |
| 250 | if (Unexpanded[I].second.isValid()) |
| 251 | Locations.push_back(Unexpanded[I].second); |
| 252 | } |
| 253 | |
Benjamin Kramer | 3a8650a | 2015-03-27 17:23:14 +0000 | [diff] [blame] | 254 | DiagnosticBuilder DB = Diag(Loc, diag::err_unexpanded_parameter_pack) |
| 255 | << (int)UPPC << (int)Names.size(); |
| 256 | for (size_t I = 0, E = std::min(Names.size(), (size_t)2); I != E; ++I) |
| 257 | DB << Names[I]; |
Douglas Gregor | 1da294a | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 258 | |
| 259 | for (unsigned I = 0, N = Locations.size(); I != N; ++I) |
| 260 | DB << SourceRange(Locations[I]); |
Richard Smith | 2589b980 | 2012-07-25 03:56:55 +0000 | [diff] [blame] | 261 | return true; |
Douglas Gregor | 1da294a | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Douglas Gregor | b55fdf8 | 2010-12-15 17:38:57 +0000 | [diff] [blame] | 264 | bool Sema::DiagnoseUnexpandedParameterPack(SourceLocation Loc, |
| 265 | TypeSourceInfo *T, |
| 266 | UnexpandedParameterPackContext UPPC) { |
| 267 | // C++0x [temp.variadic]p5: |
| 268 | // An appearance of a name of a parameter pack that is not expanded is |
| 269 | // ill-formed. |
| 270 | if (!T->getType()->containsUnexpandedParameterPack()) |
| 271 | return false; |
| 272 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 273 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
Douglas Gregor | 1da294a | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 274 | CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseTypeLoc( |
| 275 | T->getTypeLoc()); |
| 276 | assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs"); |
Richard Smith | 2589b980 | 2012-07-25 03:56:55 +0000 | [diff] [blame] | 277 | return DiagnoseUnexpandedParameterPacks(Loc, UPPC, Unexpanded); |
Douglas Gregor | b55fdf8 | 2010-12-15 17:38:57 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | bool Sema::DiagnoseUnexpandedParameterPack(Expr *E, |
Douglas Gregor | c435653 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 281 | UnexpandedParameterPackContext UPPC) { |
Douglas Gregor | b55fdf8 | 2010-12-15 17:38:57 +0000 | [diff] [blame] | 282 | // C++0x [temp.variadic]p5: |
| 283 | // An appearance of a name of a parameter pack that is not expanded is |
| 284 | // ill-formed. |
| 285 | if (!E->containsUnexpandedParameterPack()) |
| 286 | return false; |
| 287 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 288 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
Douglas Gregor | 1da294a | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 289 | CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseStmt(E); |
| 290 | assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs"); |
Richard Smith | 2589b980 | 2012-07-25 03:56:55 +0000 | [diff] [blame] | 291 | return DiagnoseUnexpandedParameterPacks(E->getLocStart(), UPPC, Unexpanded); |
Douglas Gregor | b55fdf8 | 2010-12-15 17:38:57 +0000 | [diff] [blame] | 292 | } |
Douglas Gregor | c435653 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 293 | |
| 294 | bool Sema::DiagnoseUnexpandedParameterPack(const CXXScopeSpec &SS, |
| 295 | UnexpandedParameterPackContext UPPC) { |
| 296 | // C++0x [temp.variadic]p5: |
| 297 | // An appearance of a name of a parameter pack that is not expanded is |
| 298 | // ill-formed. |
| 299 | if (!SS.getScopeRep() || |
| 300 | !SS.getScopeRep()->containsUnexpandedParameterPack()) |
| 301 | return false; |
| 302 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 303 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
Douglas Gregor | c435653 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 304 | CollectUnexpandedParameterPacksVisitor(Unexpanded) |
| 305 | .TraverseNestedNameSpecifier(SS.getScopeRep()); |
| 306 | assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs"); |
Richard Smith | 2589b980 | 2012-07-25 03:56:55 +0000 | [diff] [blame] | 307 | return DiagnoseUnexpandedParameterPacks(SS.getRange().getBegin(), |
| 308 | UPPC, Unexpanded); |
Douglas Gregor | c435653 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | bool Sema::DiagnoseUnexpandedParameterPack(const DeclarationNameInfo &NameInfo, |
| 312 | UnexpandedParameterPackContext UPPC) { |
| 313 | // C++0x [temp.variadic]p5: |
| 314 | // An appearance of a name of a parameter pack that is not expanded is |
| 315 | // ill-formed. |
| 316 | switch (NameInfo.getName().getNameKind()) { |
| 317 | case DeclarationName::Identifier: |
| 318 | case DeclarationName::ObjCZeroArgSelector: |
| 319 | case DeclarationName::ObjCOneArgSelector: |
| 320 | case DeclarationName::ObjCMultiArgSelector: |
| 321 | case DeclarationName::CXXOperatorName: |
| 322 | case DeclarationName::CXXLiteralOperatorName: |
| 323 | case DeclarationName::CXXUsingDirective: |
| 324 | return false; |
| 325 | |
| 326 | case DeclarationName::CXXConstructorName: |
| 327 | case DeclarationName::CXXDestructorName: |
| 328 | case DeclarationName::CXXConversionFunctionName: |
Douglas Gregor | 062ecac | 2010-12-16 17:19:19 +0000 | [diff] [blame] | 329 | // FIXME: We shouldn't need this null check! |
Douglas Gregor | 6ab34af | 2010-12-16 01:40:04 +0000 | [diff] [blame] | 330 | if (TypeSourceInfo *TSInfo = NameInfo.getNamedTypeInfo()) |
| 331 | return DiagnoseUnexpandedParameterPack(NameInfo.getLoc(), TSInfo, UPPC); |
| 332 | |
| 333 | if (!NameInfo.getName().getCXXNameType()->containsUnexpandedParameterPack()) |
Douglas Gregor | c435653 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 334 | return false; |
Douglas Gregor | 6ab34af | 2010-12-16 01:40:04 +0000 | [diff] [blame] | 335 | |
Douglas Gregor | c435653 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 336 | break; |
| 337 | } |
| 338 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 339 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
Douglas Gregor | c435653 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 340 | CollectUnexpandedParameterPacksVisitor(Unexpanded) |
Douglas Gregor | 6ab34af | 2010-12-16 01:40:04 +0000 | [diff] [blame] | 341 | .TraverseType(NameInfo.getName().getCXXNameType()); |
Douglas Gregor | c435653 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 342 | assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs"); |
Richard Smith | 2589b980 | 2012-07-25 03:56:55 +0000 | [diff] [blame] | 343 | return DiagnoseUnexpandedParameterPacks(NameInfo.getLoc(), UPPC, Unexpanded); |
Douglas Gregor | c435653 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 344 | } |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 345 | |
| 346 | bool Sema::DiagnoseUnexpandedParameterPack(SourceLocation Loc, |
| 347 | TemplateName Template, |
| 348 | UnexpandedParameterPackContext UPPC) { |
| 349 | |
| 350 | if (Template.isNull() || !Template.containsUnexpandedParameterPack()) |
| 351 | return false; |
| 352 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 353 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 354 | CollectUnexpandedParameterPacksVisitor(Unexpanded) |
| 355 | .TraverseTemplateName(Template); |
| 356 | assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs"); |
Richard Smith | 2589b980 | 2012-07-25 03:56:55 +0000 | [diff] [blame] | 357 | return DiagnoseUnexpandedParameterPacks(Loc, UPPC, Unexpanded); |
Douglas Gregor | 6ff1fbf | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 360 | bool Sema::DiagnoseUnexpandedParameterPack(TemplateArgumentLoc Arg, |
| 361 | UnexpandedParameterPackContext UPPC) { |
| 362 | if (Arg.getArgument().isNull() || |
| 363 | !Arg.getArgument().containsUnexpandedParameterPack()) |
| 364 | return false; |
| 365 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 366 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 367 | CollectUnexpandedParameterPacksVisitor(Unexpanded) |
| 368 | .TraverseTemplateArgumentLoc(Arg); |
| 369 | assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs"); |
Richard Smith | 2589b980 | 2012-07-25 03:56:55 +0000 | [diff] [blame] | 370 | return DiagnoseUnexpandedParameterPacks(Arg.getLocation(), UPPC, Unexpanded); |
Douglas Gregor | 1440693 | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 373 | void Sema::collectUnexpandedParameterPacks(TemplateArgument Arg, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 374 | SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) { |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 375 | CollectUnexpandedParameterPacksVisitor(Unexpanded) |
| 376 | .TraverseTemplateArgument(Arg); |
| 377 | } |
| 378 | |
Douglas Gregor | 840bd6c | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 379 | void Sema::collectUnexpandedParameterPacks(TemplateArgumentLoc Arg, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 380 | SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) { |
Douglas Gregor | 840bd6c | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 381 | CollectUnexpandedParameterPacksVisitor(Unexpanded) |
| 382 | .TraverseTemplateArgumentLoc(Arg); |
| 383 | } |
| 384 | |
Douglas Gregor | 76aca7b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 385 | void Sema::collectUnexpandedParameterPacks(QualType T, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 386 | SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) { |
Douglas Gregor | 76aca7b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 387 | CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseType(T); |
| 388 | } |
| 389 | |
Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 390 | void Sema::collectUnexpandedParameterPacks(TypeLoc TL, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 391 | SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) { |
Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 392 | CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseTypeLoc(TL); |
| 393 | } |
| 394 | |
Douglas Gregor | 4a2a8f7 | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 395 | void Sema::collectUnexpandedParameterPacks(CXXScopeSpec &SS, |
| 396 | SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) { |
| 397 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
| 398 | if (!Qualifier) |
| 399 | return; |
| 400 | |
| 401 | NestedNameSpecifierLoc QualifierLoc(Qualifier, SS.location_data()); |
| 402 | CollectUnexpandedParameterPacksVisitor(Unexpanded) |
| 403 | .TraverseNestedNameSpecifierLoc(QualifierLoc); |
| 404 | } |
| 405 | |
| 406 | void Sema::collectUnexpandedParameterPacks(const DeclarationNameInfo &NameInfo, |
| 407 | SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) { |
| 408 | CollectUnexpandedParameterPacksVisitor(Unexpanded) |
| 409 | .TraverseDeclarationNameInfo(NameInfo); |
| 410 | } |
| 411 | |
| 412 | |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 413 | ParsedTemplateArgument |
| 414 | Sema::ActOnPackExpansion(const ParsedTemplateArgument &Arg, |
| 415 | SourceLocation EllipsisLoc) { |
| 416 | if (Arg.isInvalid()) |
| 417 | return Arg; |
| 418 | |
| 419 | switch (Arg.getKind()) { |
| 420 | case ParsedTemplateArgument::Type: { |
| 421 | TypeResult Result = ActOnPackExpansion(Arg.getAsType(), EllipsisLoc); |
| 422 | if (Result.isInvalid()) |
| 423 | return ParsedTemplateArgument(); |
| 424 | |
| 425 | return ParsedTemplateArgument(Arg.getKind(), Result.get().getAsOpaquePtr(), |
| 426 | Arg.getLocation()); |
| 427 | } |
| 428 | |
Douglas Gregor | e8e9dd6 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 429 | case ParsedTemplateArgument::NonType: { |
| 430 | ExprResult Result = ActOnPackExpansion(Arg.getAsExpr(), EllipsisLoc); |
| 431 | if (Result.isInvalid()) |
| 432 | return ParsedTemplateArgument(); |
| 433 | |
| 434 | return ParsedTemplateArgument(Arg.getKind(), Result.get(), |
| 435 | Arg.getLocation()); |
| 436 | } |
| 437 | |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 438 | case ParsedTemplateArgument::Template: |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 439 | if (!Arg.getAsTemplate().get().containsUnexpandedParameterPack()) { |
| 440 | SourceRange R(Arg.getLocation()); |
| 441 | if (Arg.getScopeSpec().isValid()) |
| 442 | R.setBegin(Arg.getScopeSpec().getBeginLoc()); |
| 443 | Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
| 444 | << R; |
| 445 | return ParsedTemplateArgument(); |
| 446 | } |
| 447 | |
| 448 | return Arg.getTemplatePackExpansion(EllipsisLoc); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 449 | } |
| 450 | llvm_unreachable("Unhandled template argument kind?"); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | TypeResult Sema::ActOnPackExpansion(ParsedType Type, |
| 454 | SourceLocation EllipsisLoc) { |
| 455 | TypeSourceInfo *TSInfo; |
| 456 | GetTypeFromParser(Type, &TSInfo); |
| 457 | if (!TSInfo) |
| 458 | return true; |
| 459 | |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 460 | TypeSourceInfo *TSResult = CheckPackExpansion(TSInfo, EllipsisLoc, None); |
Douglas Gregor | 840bd6c | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 461 | if (!TSResult) |
| 462 | return true; |
| 463 | |
| 464 | return CreateParsedType(TSResult->getType(), TSResult); |
| 465 | } |
| 466 | |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 467 | TypeSourceInfo * |
| 468 | Sema::CheckPackExpansion(TypeSourceInfo *Pattern, SourceLocation EllipsisLoc, |
| 469 | Optional<unsigned> NumExpansions) { |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 470 | // Create the pack expansion type and source-location information. |
Douglas Gregor | 822d030 | 2011-01-12 17:07:58 +0000 | [diff] [blame] | 471 | QualType Result = CheckPackExpansion(Pattern->getType(), |
| 472 | Pattern->getTypeLoc().getSourceRange(), |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 473 | EllipsisLoc, NumExpansions); |
Douglas Gregor | 822d030 | 2011-01-12 17:07:58 +0000 | [diff] [blame] | 474 | if (Result.isNull()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 475 | return nullptr; |
Eli Friedman | 7152fbe | 2013-06-07 20:31:48 +0000 | [diff] [blame] | 476 | |
| 477 | TypeLocBuilder TLB; |
| 478 | TLB.pushFullCopy(Pattern->getTypeLoc()); |
| 479 | PackExpansionTypeLoc TL = TLB.push<PackExpansionTypeLoc>(Result); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 480 | TL.setEllipsisLoc(EllipsisLoc); |
Eli Friedman | 7152fbe | 2013-06-07 20:31:48 +0000 | [diff] [blame] | 481 | |
| 482 | return TLB.getTypeSourceInfo(Context, Result); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 483 | } |
Douglas Gregor | 76aca7b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 484 | |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 485 | QualType Sema::CheckPackExpansion(QualType Pattern, SourceRange PatternRange, |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 486 | SourceLocation EllipsisLoc, |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 487 | Optional<unsigned> NumExpansions) { |
Douglas Gregor | 822d030 | 2011-01-12 17:07:58 +0000 | [diff] [blame] | 488 | // C++0x [temp.variadic]p5: |
| 489 | // The pattern of a pack expansion shall name one or more |
| 490 | // parameter packs that are not expanded by a nested pack |
| 491 | // expansion. |
| 492 | if (!Pattern->containsUnexpandedParameterPack()) { |
| 493 | Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
| 494 | << PatternRange; |
| 495 | return QualType(); |
| 496 | } |
| 497 | |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 498 | return Context.getPackExpansionType(Pattern, NumExpansions); |
Douglas Gregor | 822d030 | 2011-01-12 17:07:58 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Douglas Gregor | e8e9dd6 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 501 | ExprResult Sema::ActOnPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc) { |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 502 | return CheckPackExpansion(Pattern, EllipsisLoc, None); |
Douglas Gregor | b884000 | 2011-01-14 21:20:45 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | ExprResult Sema::CheckPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc, |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 506 | Optional<unsigned> NumExpansions) { |
Douglas Gregor | e8e9dd6 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 507 | if (!Pattern) |
| 508 | return ExprError(); |
| 509 | |
| 510 | // C++0x [temp.variadic]p5: |
| 511 | // The pattern of a pack expansion shall name one or more |
| 512 | // parameter packs that are not expanded by a nested pack |
| 513 | // expansion. |
| 514 | if (!Pattern->containsUnexpandedParameterPack()) { |
| 515 | Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
| 516 | << Pattern->getSourceRange(); |
| 517 | return ExprError(); |
| 518 | } |
| 519 | |
| 520 | // Create the pack expansion expression and source-location information. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 521 | return new (Context) |
| 522 | PackExpansionExpr(Context.DependentTy, Pattern, EllipsisLoc, NumExpansions); |
Douglas Gregor | e8e9dd6 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 523 | } |
Douglas Gregor | 76aca7b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 524 | |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 525 | /// \brief Retrieve the depth and index of a parameter pack. |
| 526 | static std::pair<unsigned, unsigned> |
| 527 | getDepthAndIndex(NamedDecl *ND) { |
| 528 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND)) |
| 529 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
| 530 | |
| 531 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND)) |
| 532 | return std::make_pair(NTTP->getDepth(), NTTP->getIndex()); |
| 533 | |
| 534 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND); |
| 535 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
| 536 | } |
| 537 | |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 538 | bool Sema::CheckParameterPacksForExpansion( |
| 539 | SourceLocation EllipsisLoc, SourceRange PatternRange, |
| 540 | ArrayRef<UnexpandedParameterPack> Unexpanded, |
| 541 | const MultiLevelTemplateArgumentList &TemplateArgs, bool &ShouldExpand, |
| 542 | bool &RetainExpansion, Optional<unsigned> &NumExpansions) { |
Douglas Gregor | 76aca7b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 543 | ShouldExpand = true; |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 544 | RetainExpansion = false; |
Douglas Gregor | 76aca7b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 545 | std::pair<IdentifierInfo *, SourceLocation> FirstPack; |
| 546 | bool HaveFirstPack = false; |
| 547 | |
David Blaikie | b9c168a | 2011-09-22 02:34:54 +0000 | [diff] [blame] | 548 | for (ArrayRef<UnexpandedParameterPack>::iterator i = Unexpanded.begin(), |
| 549 | end = Unexpanded.end(); |
| 550 | i != end; ++i) { |
Douglas Gregor | 76aca7b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 551 | // Compute the depth and index for this parameter pack. |
Ted Kremenek | 582a099 | 2011-01-23 17:04:59 +0000 | [diff] [blame] | 552 | unsigned Depth = 0, Index = 0; |
Douglas Gregor | 76aca7b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 553 | IdentifierInfo *Name; |
Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 554 | bool IsFunctionParameterPack = false; |
Douglas Gregor | 76aca7b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 555 | |
| 556 | if (const TemplateTypeParmType *TTP |
David Blaikie | b9c168a | 2011-09-22 02:34:54 +0000 | [diff] [blame] | 557 | = i->first.dyn_cast<const TemplateTypeParmType *>()) { |
Douglas Gregor | 76aca7b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 558 | Depth = TTP->getDepth(); |
| 559 | Index = TTP->getIndex(); |
Chandler Carruth | dde65ea | 2011-05-01 01:05:51 +0000 | [diff] [blame] | 560 | Name = TTP->getIdentifier(); |
Douglas Gregor | 76aca7b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 561 | } else { |
David Blaikie | b9c168a | 2011-09-22 02:34:54 +0000 | [diff] [blame] | 562 | NamedDecl *ND = i->first.get<NamedDecl *>(); |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 563 | if (isa<ParmVarDecl>(ND)) |
Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 564 | IsFunctionParameterPack = true; |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 565 | else |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 566 | std::tie(Depth, Index) = getDepthAndIndex(ND); |
| 567 | |
Douglas Gregor | 76aca7b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 568 | Name = ND->getIdentifier(); |
| 569 | } |
| 570 | |
Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 571 | // Determine the size of this argument pack. |
| 572 | unsigned NewPackSize; |
| 573 | if (IsFunctionParameterPack) { |
| 574 | // Figure out whether we're instantiating to an argument pack or not. |
| 575 | typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack; |
| 576 | |
| 577 | llvm::PointerUnion<Decl *, DeclArgumentPack *> *Instantiation |
| 578 | = CurrentInstantiationScope->findInstantiationOf( |
David Blaikie | b9c168a | 2011-09-22 02:34:54 +0000 | [diff] [blame] | 579 | i->first.get<NamedDecl *>()); |
Chris Lattner | 15a776f | 2011-02-17 19:38:27 +0000 | [diff] [blame] | 580 | if (Instantiation->is<DeclArgumentPack *>()) { |
Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 581 | // We could expand this function parameter pack. |
| 582 | NewPackSize = Instantiation->get<DeclArgumentPack *>()->size(); |
| 583 | } else { |
| 584 | // We can't expand this function parameter pack, so we can't expand |
| 585 | // the pack expansion. |
| 586 | ShouldExpand = false; |
| 587 | continue; |
| 588 | } |
| 589 | } else { |
| 590 | // If we don't have a template argument at this depth/index, then we |
| 591 | // cannot expand the pack expansion. Make a note of this, but we still |
| 592 | // want to check any parameter packs we *do* have arguments for. |
| 593 | if (Depth >= TemplateArgs.getNumLevels() || |
| 594 | !TemplateArgs.hasTemplateArgument(Depth, Index)) { |
| 595 | ShouldExpand = false; |
| 596 | continue; |
| 597 | } |
| 598 | |
| 599 | // Determine the size of the argument pack. |
| 600 | NewPackSize = TemplateArgs(Depth, Index).pack_size(); |
Douglas Gregor | 76aca7b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 603 | // C++0x [temp.arg.explicit]p9: |
| 604 | // Template argument deduction can extend the sequence of template |
| 605 | // arguments corresponding to a template parameter pack, even when the |
| 606 | // sequence contains explicitly specified template arguments. |
Douglas Gregor | 63dad4d | 2011-01-20 23:15:49 +0000 | [diff] [blame] | 607 | if (!IsFunctionParameterPack) { |
| 608 | if (NamedDecl *PartialPack |
| 609 | = CurrentInstantiationScope->getPartiallySubstitutedPack()){ |
| 610 | unsigned PartialDepth, PartialIndex; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 611 | std::tie(PartialDepth, PartialIndex) = getDepthAndIndex(PartialPack); |
Douglas Gregor | 63dad4d | 2011-01-20 23:15:49 +0000 | [diff] [blame] | 612 | if (PartialDepth == Depth && PartialIndex == Index) |
| 613 | RetainExpansion = true; |
| 614 | } |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 615 | } |
Douglas Gregor | 63dad4d | 2011-01-20 23:15:49 +0000 | [diff] [blame] | 616 | |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 617 | if (!NumExpansions) { |
Douglas Gregor | 76aca7b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 618 | // The is the first pack we've seen for which we have an argument. |
| 619 | // Record it. |
| 620 | NumExpansions = NewPackSize; |
| 621 | FirstPack.first = Name; |
David Blaikie | b9c168a | 2011-09-22 02:34:54 +0000 | [diff] [blame] | 622 | FirstPack.second = i->second; |
Douglas Gregor | 76aca7b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 623 | HaveFirstPack = true; |
| 624 | continue; |
| 625 | } |
| 626 | |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 627 | if (NewPackSize != *NumExpansions) { |
Douglas Gregor | 76aca7b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 628 | // C++0x [temp.variadic]p5: |
| 629 | // All of the parameter packs expanded by a pack expansion shall have |
| 630 | // the same number of arguments specified. |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 631 | if (HaveFirstPack) |
| 632 | Diag(EllipsisLoc, diag::err_pack_expansion_length_conflict) |
| 633 | << FirstPack.first << Name << *NumExpansions << NewPackSize |
David Blaikie | b9c168a | 2011-09-22 02:34:54 +0000 | [diff] [blame] | 634 | << SourceRange(FirstPack.second) << SourceRange(i->second); |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 635 | else |
| 636 | Diag(EllipsisLoc, diag::err_pack_expansion_length_conflict_multilevel) |
| 637 | << Name << *NumExpansions << NewPackSize |
David Blaikie | b9c168a | 2011-09-22 02:34:54 +0000 | [diff] [blame] | 638 | << SourceRange(i->second); |
Douglas Gregor | 76aca7b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 639 | return true; |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | return false; |
| 644 | } |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 645 | |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 646 | Optional<unsigned> Sema::getNumArgumentsInExpansion(QualType T, |
Douglas Gregor | 5cde386 | 2011-01-11 03:14:20 +0000 | [diff] [blame] | 647 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 648 | QualType Pattern = cast<PackExpansionType>(T)->getPattern(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 649 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
Douglas Gregor | 5cde386 | 2011-01-11 03:14:20 +0000 | [diff] [blame] | 650 | CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseType(Pattern); |
| 651 | |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 652 | Optional<unsigned> Result; |
Douglas Gregor | 5cde386 | 2011-01-11 03:14:20 +0000 | [diff] [blame] | 653 | for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) { |
| 654 | // Compute the depth and index for this parameter pack. |
| 655 | unsigned Depth; |
| 656 | unsigned Index; |
| 657 | |
| 658 | if (const TemplateTypeParmType *TTP |
| 659 | = Unexpanded[I].first.dyn_cast<const TemplateTypeParmType *>()) { |
| 660 | Depth = TTP->getDepth(); |
| 661 | Index = TTP->getIndex(); |
| 662 | } else { |
| 663 | NamedDecl *ND = Unexpanded[I].first.get<NamedDecl *>(); |
| 664 | if (isa<ParmVarDecl>(ND)) { |
| 665 | // Function parameter pack. |
| 666 | typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack; |
| 667 | |
| 668 | llvm::PointerUnion<Decl *, DeclArgumentPack *> *Instantiation |
| 669 | = CurrentInstantiationScope->findInstantiationOf( |
| 670 | Unexpanded[I].first.get<NamedDecl *>()); |
Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 671 | if (Instantiation->is<Decl*>()) |
| 672 | // The pattern refers to an unexpanded pack. We're not ready to expand |
| 673 | // this pack yet. |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 674 | return None; |
Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 675 | |
| 676 | unsigned Size = Instantiation->get<DeclArgumentPack *>()->size(); |
| 677 | assert((!Result || *Result == Size) && "inconsistent pack sizes"); |
| 678 | Result = Size; |
Douglas Gregor | 5cde386 | 2011-01-11 03:14:20 +0000 | [diff] [blame] | 679 | continue; |
| 680 | } |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 681 | |
| 682 | std::tie(Depth, Index) = getDepthAndIndex(ND); |
Douglas Gregor | 5cde386 | 2011-01-11 03:14:20 +0000 | [diff] [blame] | 683 | } |
| 684 | if (Depth >= TemplateArgs.getNumLevels() || |
| 685 | !TemplateArgs.hasTemplateArgument(Depth, Index)) |
Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 686 | // The pattern refers to an unknown template argument. We're not ready to |
| 687 | // expand this pack yet. |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 688 | return None; |
Douglas Gregor | 5cde386 | 2011-01-11 03:14:20 +0000 | [diff] [blame] | 689 | |
| 690 | // Determine the size of the argument pack. |
Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 691 | unsigned Size = TemplateArgs(Depth, Index).pack_size(); |
| 692 | assert((!Result || *Result == Size) && "inconsistent pack sizes"); |
| 693 | Result = Size; |
Douglas Gregor | 5cde386 | 2011-01-11 03:14:20 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 696 | return Result; |
Douglas Gregor | 5cde386 | 2011-01-11 03:14:20 +0000 | [diff] [blame] | 697 | } |
| 698 | |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 699 | bool Sema::containsUnexpandedParameterPacks(Declarator &D) { |
| 700 | const DeclSpec &DS = D.getDeclSpec(); |
| 701 | switch (DS.getTypeSpecType()) { |
| 702 | case TST_typename: |
Alexis Hunt | 4a25707 | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 703 | case TST_typeofType: |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 704 | case TST_underlyingType: |
| 705 | case TST_atomic: { |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 706 | QualType T = DS.getRepAsType().get(); |
| 707 | if (!T.isNull() && T->containsUnexpandedParameterPack()) |
| 708 | return true; |
| 709 | break; |
| 710 | } |
| 711 | |
| 712 | case TST_typeofExpr: |
| 713 | case TST_decltype: |
| 714 | if (DS.getRepAsExpr() && |
| 715 | DS.getRepAsExpr()->containsUnexpandedParameterPack()) |
| 716 | return true; |
| 717 | break; |
| 718 | |
| 719 | case TST_unspecified: |
| 720 | case TST_void: |
| 721 | case TST_char: |
| 722 | case TST_wchar: |
| 723 | case TST_char16: |
| 724 | case TST_char32: |
| 725 | case TST_int: |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 726 | case TST_int128: |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 727 | case TST_half: |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 728 | case TST_float: |
| 729 | case TST_double: |
| 730 | case TST_bool: |
| 731 | case TST_decimal32: |
| 732 | case TST_decimal64: |
| 733 | case TST_decimal128: |
| 734 | case TST_enum: |
| 735 | case TST_union: |
| 736 | case TST_struct: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 737 | case TST_interface: |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 738 | case TST_class: |
| 739 | case TST_auto: |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 740 | case TST_auto_type: |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 741 | case TST_decltype_auto: |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame^] | 742 | #define GENERIC_IMAGE_TYPE(ImgType, Id) case TST_##ImgType##_t: |
| 743 | #include "clang/AST/OpenCLImageTypes.def" |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 744 | case TST_unknown_anytype: |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 745 | case TST_error: |
| 746 | break; |
| 747 | } |
Larisse Voufo | 2e84650 | 2014-08-29 21:08:16 +0000 | [diff] [blame] | 748 | |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 749 | for (unsigned I = 0, N = D.getNumTypeObjects(); I != N; ++I) { |
| 750 | const DeclaratorChunk &Chunk = D.getTypeObject(I); |
| 751 | switch (Chunk.Kind) { |
| 752 | case DeclaratorChunk::Pointer: |
| 753 | case DeclaratorChunk::Reference: |
| 754 | case DeclaratorChunk::Paren: |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 755 | case DeclaratorChunk::Pipe: |
Larisse Voufo | 2e84650 | 2014-08-29 21:08:16 +0000 | [diff] [blame] | 756 | case DeclaratorChunk::BlockPointer: |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 757 | // These declarator chunks cannot contain any parameter packs. |
| 758 | break; |
| 759 | |
| 760 | case DeclaratorChunk::Array: |
Larisse Voufo | 2e84650 | 2014-08-29 21:08:16 +0000 | [diff] [blame] | 761 | if (Chunk.Arr.NumElts && |
| 762 | Chunk.Arr.NumElts->containsUnexpandedParameterPack()) |
| 763 | return true; |
| 764 | break; |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 765 | case DeclaratorChunk::Function: |
Larisse Voufo | 2e84650 | 2014-08-29 21:08:16 +0000 | [diff] [blame] | 766 | for (unsigned i = 0, e = Chunk.Fun.NumParams; i != e; ++i) { |
| 767 | ParmVarDecl *Param = cast<ParmVarDecl>(Chunk.Fun.Params[i].Param); |
| 768 | QualType ParamTy = Param->getType(); |
| 769 | assert(!ParamTy.isNull() && "Couldn't parse type?"); |
| 770 | if (ParamTy->containsUnexpandedParameterPack()) return true; |
| 771 | } |
| 772 | |
| 773 | if (Chunk.Fun.getExceptionSpecType() == EST_Dynamic) { |
| 774 | for (unsigned i = 0; i != Chunk.Fun.NumExceptions; ++i) { |
| 775 | if (Chunk.Fun.Exceptions[i] |
| 776 | .Ty.get() |
| 777 | ->containsUnexpandedParameterPack()) |
| 778 | return true; |
| 779 | } |
| 780 | } else if (Chunk.Fun.getExceptionSpecType() == EST_ComputedNoexcept && |
| 781 | Chunk.Fun.NoexceptExpr->containsUnexpandedParameterPack()) |
| 782 | return true; |
| 783 | |
Nico Weber | 8d26b72 | 2014-12-30 02:06:40 +0000 | [diff] [blame] | 784 | if (Chunk.Fun.hasTrailingReturnType()) { |
| 785 | QualType T = Chunk.Fun.getTrailingReturnType().get(); |
| 786 | if (!T.isNull() && T->containsUnexpandedParameterPack()) |
| 787 | return true; |
| 788 | } |
Larisse Voufo | 2e84650 | 2014-08-29 21:08:16 +0000 | [diff] [blame] | 789 | break; |
| 790 | |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 791 | case DeclaratorChunk::MemberPointer: |
| 792 | if (Chunk.Mem.Scope().getScopeRep() && |
| 793 | Chunk.Mem.Scope().getScopeRep()->containsUnexpandedParameterPack()) |
| 794 | return true; |
| 795 | break; |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | return false; |
| 800 | } |
Douglas Gregor | 820ba7b | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 801 | |
Kaelyn Uhrain | 637b5b3 | 2012-01-13 23:10:36 +0000 | [diff] [blame] | 802 | namespace { |
| 803 | |
| 804 | // Callback to only accept typo corrections that refer to parameter packs. |
| 805 | class ParameterPackValidatorCCC : public CorrectionCandidateCallback { |
| 806 | public: |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 807 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
Kaelyn Uhrain | 637b5b3 | 2012-01-13 23:10:36 +0000 | [diff] [blame] | 808 | NamedDecl *ND = candidate.getCorrectionDecl(); |
| 809 | return ND && ND->isParameterPack(); |
| 810 | } |
| 811 | }; |
| 812 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 813 | } |
Kaelyn Uhrain | 637b5b3 | 2012-01-13 23:10:36 +0000 | [diff] [blame] | 814 | |
Douglas Gregor | 820ba7b | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 815 | /// \brief Called when an expression computing the size of a parameter pack |
| 816 | /// is parsed. |
| 817 | /// |
| 818 | /// \code |
| 819 | /// template<typename ...Types> struct count { |
| 820 | /// static const unsigned value = sizeof...(Types); |
| 821 | /// }; |
| 822 | /// \endcode |
| 823 | /// |
| 824 | // |
| 825 | /// \param OpLoc The location of the "sizeof" keyword. |
| 826 | /// \param Name The name of the parameter pack whose size will be determined. |
| 827 | /// \param NameLoc The source location of the name of the parameter pack. |
| 828 | /// \param RParenLoc The location of the closing parentheses. |
| 829 | ExprResult Sema::ActOnSizeofParameterPackExpr(Scope *S, |
| 830 | SourceLocation OpLoc, |
| 831 | IdentifierInfo &Name, |
| 832 | SourceLocation NameLoc, |
| 833 | SourceLocation RParenLoc) { |
| 834 | // C++0x [expr.sizeof]p5: |
| 835 | // The identifier in a sizeof... expression shall name a parameter pack. |
Douglas Gregor | 820ba7b | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 836 | LookupResult R(*this, &Name, NameLoc, LookupOrdinaryName); |
| 837 | LookupName(R, S); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 838 | |
| 839 | NamedDecl *ParameterPack = nullptr; |
Douglas Gregor | 820ba7b | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 840 | switch (R.getResultKind()) { |
| 841 | case LookupResult::Found: |
| 842 | ParameterPack = R.getFoundDecl(); |
| 843 | break; |
| 844 | |
| 845 | case LookupResult::NotFound: |
| 846 | case LookupResult::NotFoundInCurrentInstantiation: |
Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 847 | if (TypoCorrection Corrected = |
| 848 | CorrectTypo(R.getLookupNameInfo(), R.getLookupKind(), S, nullptr, |
| 849 | llvm::make_unique<ParameterPackValidatorCCC>(), |
| 850 | CTK_ErrorRecovery)) { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 851 | diagnoseTypo(Corrected, |
| 852 | PDiag(diag::err_sizeof_pack_no_pack_name_suggest) << &Name, |
| 853 | PDiag(diag::note_parameter_pack_here)); |
Kaelyn Uhrain | 637b5b3 | 2012-01-13 23:10:36 +0000 | [diff] [blame] | 854 | ParameterPack = Corrected.getCorrectionDecl(); |
Douglas Gregor | 820ba7b | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 855 | } |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 856 | |
Douglas Gregor | 820ba7b | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 857 | case LookupResult::FoundOverloaded: |
| 858 | case LookupResult::FoundUnresolvedValue: |
| 859 | break; |
| 860 | |
| 861 | case LookupResult::Ambiguous: |
| 862 | DiagnoseAmbiguousLookup(R); |
| 863 | return ExprError(); |
| 864 | } |
| 865 | |
Douglas Gregor | 3c6bd2a | 2011-01-05 21:11:38 +0000 | [diff] [blame] | 866 | if (!ParameterPack || !ParameterPack->isParameterPack()) { |
Douglas Gregor | 820ba7b | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 867 | Diag(NameLoc, diag::err_sizeof_pack_no_pack_name) |
| 868 | << &Name; |
| 869 | return ExprError(); |
| 870 | } |
| 871 | |
Nick Lewycky | 45b5052 | 2013-02-02 00:25:55 +0000 | [diff] [blame] | 872 | MarkAnyDeclReferenced(OpLoc, ParameterPack, true); |
Eli Friedman | 23b1be9 | 2012-03-01 21:32:56 +0000 | [diff] [blame] | 873 | |
Richard Smith | d784e68 | 2015-09-23 21:41:42 +0000 | [diff] [blame] | 874 | return SizeOfPackExpr::Create(Context, OpLoc, ParameterPack, NameLoc, |
| 875 | RParenLoc); |
Douglas Gregor | 820ba7b | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 876 | } |
Eli Friedman | 94e9eaa | 2013-06-20 04:11:21 +0000 | [diff] [blame] | 877 | |
| 878 | TemplateArgumentLoc |
| 879 | Sema::getTemplateArgumentPackExpansionPattern( |
| 880 | TemplateArgumentLoc OrigLoc, |
| 881 | SourceLocation &Ellipsis, Optional<unsigned> &NumExpansions) const { |
| 882 | const TemplateArgument &Argument = OrigLoc.getArgument(); |
| 883 | assert(Argument.isPackExpansion()); |
| 884 | switch (Argument.getKind()) { |
| 885 | case TemplateArgument::Type: { |
| 886 | // FIXME: We shouldn't ever have to worry about missing |
| 887 | // type-source info! |
| 888 | TypeSourceInfo *ExpansionTSInfo = OrigLoc.getTypeSourceInfo(); |
| 889 | if (!ExpansionTSInfo) |
| 890 | ExpansionTSInfo = Context.getTrivialTypeSourceInfo(Argument.getAsType(), |
| 891 | Ellipsis); |
| 892 | PackExpansionTypeLoc Expansion = |
| 893 | ExpansionTSInfo->getTypeLoc().castAs<PackExpansionTypeLoc>(); |
| 894 | Ellipsis = Expansion.getEllipsisLoc(); |
| 895 | |
| 896 | TypeLoc Pattern = Expansion.getPatternLoc(); |
| 897 | NumExpansions = Expansion.getTypePtr()->getNumExpansions(); |
| 898 | |
| 899 | // We need to copy the TypeLoc because TemplateArgumentLocs store a |
| 900 | // TypeSourceInfo. |
| 901 | // FIXME: Find some way to avoid the copy? |
| 902 | TypeLocBuilder TLB; |
| 903 | TLB.pushFullCopy(Pattern); |
| 904 | TypeSourceInfo *PatternTSInfo = |
| 905 | TLB.getTypeSourceInfo(Context, Pattern.getType()); |
| 906 | return TemplateArgumentLoc(TemplateArgument(Pattern.getType()), |
| 907 | PatternTSInfo); |
| 908 | } |
| 909 | |
| 910 | case TemplateArgument::Expression: { |
| 911 | PackExpansionExpr *Expansion |
| 912 | = cast<PackExpansionExpr>(Argument.getAsExpr()); |
| 913 | Expr *Pattern = Expansion->getPattern(); |
| 914 | Ellipsis = Expansion->getEllipsisLoc(); |
| 915 | NumExpansions = Expansion->getNumExpansions(); |
| 916 | return TemplateArgumentLoc(Pattern, Pattern); |
| 917 | } |
| 918 | |
| 919 | case TemplateArgument::TemplateExpansion: |
| 920 | Ellipsis = OrigLoc.getTemplateEllipsisLoc(); |
| 921 | NumExpansions = Argument.getNumTemplateExpansions(); |
| 922 | return TemplateArgumentLoc(Argument.getPackExpansionPattern(), |
| 923 | OrigLoc.getTemplateQualifierLoc(), |
| 924 | OrigLoc.getTemplateNameLoc()); |
| 925 | |
| 926 | case TemplateArgument::Declaration: |
| 927 | case TemplateArgument::NullPtr: |
| 928 | case TemplateArgument::Template: |
| 929 | case TemplateArgument::Integral: |
| 930 | case TemplateArgument::Pack: |
| 931 | case TemplateArgument::Null: |
| 932 | return TemplateArgumentLoc(); |
| 933 | } |
| 934 | |
| 935 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
| 936 | } |
Richard Smith | 0f0af19 | 2014-11-08 05:07:16 +0000 | [diff] [blame] | 937 | |
| 938 | static void CheckFoldOperand(Sema &S, Expr *E) { |
| 939 | if (!E) |
| 940 | return; |
| 941 | |
| 942 | E = E->IgnoreImpCasts(); |
| 943 | if (isa<BinaryOperator>(E) || isa<AbstractConditionalOperator>(E)) { |
| 944 | S.Diag(E->getExprLoc(), diag::err_fold_expression_bad_operand) |
| 945 | << E->getSourceRange() |
| 946 | << FixItHint::CreateInsertion(E->getLocStart(), "(") |
| 947 | << FixItHint::CreateInsertion(E->getLocEnd(), ")"); |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | ExprResult Sema::ActOnCXXFoldExpr(SourceLocation LParenLoc, Expr *LHS, |
| 952 | tok::TokenKind Operator, |
| 953 | SourceLocation EllipsisLoc, Expr *RHS, |
| 954 | SourceLocation RParenLoc) { |
| 955 | // LHS and RHS must be cast-expressions. We allow an arbitrary expression |
| 956 | // in the parser and reduce down to just cast-expressions here. |
| 957 | CheckFoldOperand(*this, LHS); |
| 958 | CheckFoldOperand(*this, RHS); |
| 959 | |
| 960 | // [expr.prim.fold]p3: |
| 961 | // In a binary fold, op1 and op2 shall be the same fold-operator, and |
| 962 | // either e1 shall contain an unexpanded parameter pack or e2 shall contain |
| 963 | // an unexpanded parameter pack, but not both. |
| 964 | if (LHS && RHS && |
| 965 | LHS->containsUnexpandedParameterPack() == |
| 966 | RHS->containsUnexpandedParameterPack()) { |
| 967 | return Diag(EllipsisLoc, |
| 968 | LHS->containsUnexpandedParameterPack() |
| 969 | ? diag::err_fold_expression_packs_both_sides |
| 970 | : diag::err_pack_expansion_without_parameter_packs) |
| 971 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 972 | } |
| 973 | |
| 974 | // [expr.prim.fold]p2: |
| 975 | // In a unary fold, the cast-expression shall contain an unexpanded |
| 976 | // parameter pack. |
| 977 | if (!LHS || !RHS) { |
| 978 | Expr *Pack = LHS ? LHS : RHS; |
| 979 | assert(Pack && "fold expression with neither LHS nor RHS"); |
| 980 | if (!Pack->containsUnexpandedParameterPack()) |
| 981 | return Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
| 982 | << Pack->getSourceRange(); |
| 983 | } |
| 984 | |
| 985 | BinaryOperatorKind Opc = ConvertTokenKindToBinaryOpcode(Operator); |
| 986 | return BuildCXXFoldExpr(LParenLoc, LHS, Opc, EllipsisLoc, RHS, RParenLoc); |
| 987 | } |
| 988 | |
| 989 | ExprResult Sema::BuildCXXFoldExpr(SourceLocation LParenLoc, Expr *LHS, |
| 990 | BinaryOperatorKind Operator, |
| 991 | SourceLocation EllipsisLoc, Expr *RHS, |
| 992 | SourceLocation RParenLoc) { |
| 993 | return new (Context) CXXFoldExpr(Context.DependentTy, LParenLoc, LHS, |
| 994 | Operator, EllipsisLoc, RHS, RParenLoc); |
| 995 | } |
| 996 | |
| 997 | ExprResult Sema::BuildEmptyCXXFoldExpr(SourceLocation EllipsisLoc, |
| 998 | BinaryOperatorKind Operator) { |
| 999 | // [temp.variadic]p9: |
| 1000 | // If N is zero for a unary fold-expression, the value of the expression is |
Richard Smith | 0f0af19 | 2014-11-08 05:07:16 +0000 | [diff] [blame] | 1001 | // && -> true |
| 1002 | // || -> false |
| 1003 | // , -> void() |
| 1004 | // if the operator is not listed [above], the instantiation is ill-formed. |
| 1005 | // |
| 1006 | // Note that we need to use something like int() here, not merely 0, to |
| 1007 | // prevent the result from being a null pointer constant. |
| 1008 | QualType ScalarType; |
| 1009 | switch (Operator) { |
Richard Smith | 0f0af19 | 2014-11-08 05:07:16 +0000 | [diff] [blame] | 1010 | case BO_LOr: |
| 1011 | return ActOnCXXBoolLiteral(EllipsisLoc, tok::kw_false); |
| 1012 | case BO_LAnd: |
| 1013 | return ActOnCXXBoolLiteral(EllipsisLoc, tok::kw_true); |
| 1014 | case BO_Comma: |
| 1015 | ScalarType = Context.VoidTy; |
| 1016 | break; |
| 1017 | |
| 1018 | default: |
| 1019 | return Diag(EllipsisLoc, diag::err_fold_expression_empty) |
| 1020 | << BinaryOperator::getOpcodeStr(Operator); |
| 1021 | } |
| 1022 | |
| 1023 | return new (Context) CXXScalarValueInitExpr( |
| 1024 | ScalarType, Context.getTrivialTypeSourceInfo(ScalarType, EllipsisLoc), |
| 1025 | EllipsisLoc); |
| 1026 | } |