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