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