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