Chris Lattner | 2937565 | 2006-12-04 18:06:35 +0000 | [diff] [blame] | 1 | //===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Chris Lattner | 2937565 | 2006-12-04 18:06:35 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the Expression parsing implementation for C++. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
Vassil Vassilev | 11ad339 | 2017-03-23 15:11:07 +0000 | [diff] [blame] | 12 | #include "clang/Parse/Parser.h" |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 13 | #include "clang/AST/ASTContext.h" |
Chandler Carruth | 757fcd6 | 2014-03-04 10:05:20 +0000 | [diff] [blame] | 14 | #include "clang/AST/DeclTemplate.h" |
Eli Friedman | c7c9714 | 2012-01-04 02:40:39 +0000 | [diff] [blame] | 15 | #include "clang/Basic/PrettyStackTrace.h" |
Richard Smith | 7d182a7 | 2012-03-08 23:06:02 +0000 | [diff] [blame] | 16 | #include "clang/Lex/LiteralSupport.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 17 | #include "clang/Parse/ParseDiagnostic.h" |
Vassil Vassilev | 11ad339 | 2017-03-23 15:11:07 +0000 | [diff] [blame] | 18 | #include "clang/Parse/RAIIObjectsForParser.h" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 19 | #include "clang/Sema/DeclSpec.h" |
| 20 | #include "clang/Sema/ParsedTemplate.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 21 | #include "clang/Sema/Scope.h" |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ErrorHandling.h" |
| 23 | |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 24 | |
Chris Lattner | 2937565 | 2006-12-04 18:06:35 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | |
Alp Toker | f990cef | 2014-01-07 02:35:33 +0000 | [diff] [blame] | 27 | static int SelectDigraphErrorMessage(tok::TokenKind Kind) { |
| 28 | switch (Kind) { |
| 29 | // template name |
| 30 | case tok::unknown: return 0; |
| 31 | // casts |
| 32 | case tok::kw_const_cast: return 1; |
| 33 | case tok::kw_dynamic_cast: return 2; |
| 34 | case tok::kw_reinterpret_cast: return 3; |
| 35 | case tok::kw_static_cast: return 4; |
| 36 | default: |
| 37 | llvm_unreachable("Unknown type for digraph error message."); |
| 38 | } |
| 39 | } |
| 40 | |
Richard Smith | 5585849 | 2011-04-14 21:45:45 +0000 | [diff] [blame] | 41 | // Are the two tokens adjacent in the same source file? |
Richard Smith | 7b3f322 | 2012-06-18 06:11:04 +0000 | [diff] [blame] | 42 | bool Parser::areTokensAdjacent(const Token &First, const Token &Second) { |
Richard Smith | 5585849 | 2011-04-14 21:45:45 +0000 | [diff] [blame] | 43 | SourceManager &SM = PP.getSourceManager(); |
| 44 | SourceLocation FirstLoc = SM.getSpellingLoc(First.getLocation()); |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 45 | SourceLocation FirstEnd = FirstLoc.getLocWithOffset(First.getLength()); |
Richard Smith | 5585849 | 2011-04-14 21:45:45 +0000 | [diff] [blame] | 46 | return FirstEnd == SM.getSpellingLoc(Second.getLocation()); |
| 47 | } |
| 48 | |
| 49 | // Suggest fixit for "<::" after a cast. |
| 50 | static void FixDigraph(Parser &P, Preprocessor &PP, Token &DigraphToken, |
| 51 | Token &ColonToken, tok::TokenKind Kind, bool AtDigraph) { |
| 52 | // Pull '<:' and ':' off token stream. |
| 53 | if (!AtDigraph) |
| 54 | PP.Lex(DigraphToken); |
| 55 | PP.Lex(ColonToken); |
| 56 | |
| 57 | SourceRange Range; |
| 58 | Range.setBegin(DigraphToken.getLocation()); |
| 59 | Range.setEnd(ColonToken.getLocation()); |
| 60 | P.Diag(DigraphToken.getLocation(), diag::err_missing_whitespace_digraph) |
Alp Toker | f990cef | 2014-01-07 02:35:33 +0000 | [diff] [blame] | 61 | << SelectDigraphErrorMessage(Kind) |
| 62 | << FixItHint::CreateReplacement(Range, "< ::"); |
Richard Smith | 5585849 | 2011-04-14 21:45:45 +0000 | [diff] [blame] | 63 | |
| 64 | // Update token information to reflect their change in token type. |
| 65 | ColonToken.setKind(tok::coloncolon); |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 66 | ColonToken.setLocation(ColonToken.getLocation().getLocWithOffset(-1)); |
Richard Smith | 5585849 | 2011-04-14 21:45:45 +0000 | [diff] [blame] | 67 | ColonToken.setLength(2); |
| 68 | DigraphToken.setKind(tok::less); |
| 69 | DigraphToken.setLength(1); |
| 70 | |
| 71 | // Push new tokens back to token stream. |
Ilya Biryukov | 929af67 | 2019-05-17 09:32:05 +0000 | [diff] [blame^] | 72 | PP.EnterToken(ColonToken, /*IsReinject*/ true); |
Richard Smith | 5585849 | 2011-04-14 21:45:45 +0000 | [diff] [blame] | 73 | if (!AtDigraph) |
Ilya Biryukov | 929af67 | 2019-05-17 09:32:05 +0000 | [diff] [blame^] | 74 | PP.EnterToken(DigraphToken, /*IsReinject*/ true); |
Richard Smith | 5585849 | 2011-04-14 21:45:45 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Richard Trieu | 01fc001 | 2011-09-19 19:01:00 +0000 | [diff] [blame] | 77 | // Check for '<::' which should be '< ::' instead of '[:' when following |
| 78 | // a template name. |
| 79 | void Parser::CheckForTemplateAndDigraph(Token &Next, ParsedType ObjectType, |
| 80 | bool EnteringContext, |
| 81 | IdentifierInfo &II, CXXScopeSpec &SS) { |
Richard Trieu | 02e25db | 2011-09-20 20:03:50 +0000 | [diff] [blame] | 82 | if (!Next.is(tok::l_square) || Next.getLength() != 2) |
Richard Trieu | 01fc001 | 2011-09-19 19:01:00 +0000 | [diff] [blame] | 83 | return; |
| 84 | |
| 85 | Token SecondToken = GetLookAheadToken(2); |
Richard Smith | 7b3f322 | 2012-06-18 06:11:04 +0000 | [diff] [blame] | 86 | if (!SecondToken.is(tok::colon) || !areTokensAdjacent(Next, SecondToken)) |
Richard Trieu | 01fc001 | 2011-09-19 19:01:00 +0000 | [diff] [blame] | 87 | return; |
| 88 | |
| 89 | TemplateTy Template; |
| 90 | UnqualifiedId TemplateName; |
| 91 | TemplateName.setIdentifier(&II, Tok.getLocation()); |
| 92 | bool MemberOfUnknownSpecialization; |
| 93 | if (!Actions.isTemplateName(getCurScope(), SS, /*hasTemplateKeyword=*/false, |
| 94 | TemplateName, ObjectType, EnteringContext, |
| 95 | Template, MemberOfUnknownSpecialization)) |
| 96 | return; |
| 97 | |
Alp Toker | f990cef | 2014-01-07 02:35:33 +0000 | [diff] [blame] | 98 | FixDigraph(*this, PP, Next, SecondToken, tok::unknown, |
| 99 | /*AtDigraph*/false); |
Richard Trieu | 01fc001 | 2011-09-19 19:01:00 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 102 | /// Parse global scope or nested-name-specifier if present. |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 103 | /// |
| 104 | /// Parses a C++ global scope specifier ('::') or nested-name-specifier (which |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 105 | /// may be preceded by '::'). Note that this routine will not parse ::new or |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 106 | /// ::delete; it will just leave them in the token stream. |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 107 | /// |
| 108 | /// '::'[opt] nested-name-specifier |
| 109 | /// '::' |
| 110 | /// |
| 111 | /// nested-name-specifier: |
| 112 | /// type-name '::' |
| 113 | /// namespace-name '::' |
| 114 | /// nested-name-specifier identifier '::' |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 115 | /// nested-name-specifier 'template'[opt] simple-template-id '::' |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 116 | /// |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 117 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 118 | /// \param SS the scope specifier that will be set to the parsed |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 119 | /// nested-name-specifier (or empty) |
| 120 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 121 | /// \param ObjectType if this nested-name-specifier is being parsed following |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 122 | /// the "." or "->" of a member access expression, this parameter provides the |
| 123 | /// type of the object whose members are being accessed. |
| 124 | /// |
| 125 | /// \param EnteringContext whether we will be entering into the context of |
| 126 | /// the nested-name-specifier after parsing it. |
| 127 | /// |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 128 | /// \param MayBePseudoDestructor When non-NULL, points to a flag that |
| 129 | /// indicates whether this nested-name-specifier may be part of a |
| 130 | /// pseudo-destructor name. In this case, the flag will be set false |
| 131 | /// if we don't actually end up parsing a destructor name. Moreorover, |
| 132 | /// if we do end up determining that we are parsing a destructor name, |
| 133 | /// the last component of the nested-name-specifier is not parsed as |
| 134 | /// part of the scope specifier. |
Richard Smith | 7447af4 | 2013-03-26 01:15:19 +0000 | [diff] [blame] | 135 | /// |
| 136 | /// \param IsTypename If \c true, this nested-name-specifier is known to be |
| 137 | /// part of a type name. This is used to improve error recovery. |
| 138 | /// |
| 139 | /// \param LastII When non-NULL, points to an IdentifierInfo* that will be |
| 140 | /// filled in with the leading identifier in the last component of the |
| 141 | /// nested-name-specifier, if any. |
Douglas Gregor | 90d554e | 2010-02-21 18:36:56 +0000 | [diff] [blame] | 142 | /// |
Matthias Gehre | dc01bb4 | 2017-03-17 21:41:20 +0000 | [diff] [blame] | 143 | /// \param OnlyNamespace If true, only considers namespaces in lookup. |
| 144 | /// |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 145 | /// \returns true if there was an error parsing a scope specifier |
Douglas Gregor | e861bac | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 146 | bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 147 | ParsedType ObjectType, |
Douglas Gregor | 90d554e | 2010-02-21 18:36:56 +0000 | [diff] [blame] | 148 | bool EnteringContext, |
Francois Pichet | 4e7a2c0 | 2011-03-27 19:41:34 +0000 | [diff] [blame] | 149 | bool *MayBePseudoDestructor, |
Richard Smith | 7447af4 | 2013-03-26 01:15:19 +0000 | [diff] [blame] | 150 | bool IsTypename, |
Matthias Gehre | dc01bb4 | 2017-03-17 21:41:20 +0000 | [diff] [blame] | 151 | IdentifierInfo **LastII, |
| 152 | bool OnlyNamespace) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 153 | assert(getLangOpts().CPlusPlus && |
Chris Lattner | b5134c0 | 2009-01-05 01:24:05 +0000 | [diff] [blame] | 154 | "Call sites of this function should be guarded by checking for C++"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 156 | if (Tok.is(tok::annot_cxxscope)) { |
Richard Smith | 7447af4 | 2013-03-26 01:15:19 +0000 | [diff] [blame] | 157 | assert(!LastII && "want last identifier but have already annotated scope"); |
Nico Weber | c60aa71 | 2015-02-16 22:32:46 +0000 | [diff] [blame] | 158 | assert(!MayBePseudoDestructor && "unexpected annot_cxxscope"); |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 159 | Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(), |
| 160 | Tok.getAnnotationRange(), |
| 161 | SS); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 162 | ConsumeAnnotationToken(); |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 163 | return false; |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 164 | } |
Chris Lattner | f9b2cd4 | 2009-01-04 21:14:15 +0000 | [diff] [blame] | 165 | |
Larisse Voufo | b959c3c | 2013-08-06 05:49:26 +0000 | [diff] [blame] | 166 | if (Tok.is(tok::annot_template_id)) { |
| 167 | // If the current token is an annotated template id, it may already have |
| 168 | // a scope specifier. Restore it. |
| 169 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
| 170 | SS = TemplateId->SS; |
| 171 | } |
| 172 | |
Nico Weber | c60aa71 | 2015-02-16 22:32:46 +0000 | [diff] [blame] | 173 | // Has to happen before any "return false"s in this function. |
| 174 | bool CheckForDestructor = false; |
| 175 | if (MayBePseudoDestructor && *MayBePseudoDestructor) { |
| 176 | CheckForDestructor = true; |
| 177 | *MayBePseudoDestructor = false; |
| 178 | } |
| 179 | |
Richard Smith | 7447af4 | 2013-03-26 01:15:19 +0000 | [diff] [blame] | 180 | if (LastII) |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 181 | *LastII = nullptr; |
Richard Smith | 7447af4 | 2013-03-26 01:15:19 +0000 | [diff] [blame] | 182 | |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 183 | bool HasScopeSpecifier = false; |
| 184 | |
Chris Lattner | 8a7d10d | 2009-01-05 03:55:46 +0000 | [diff] [blame] | 185 | if (Tok.is(tok::coloncolon)) { |
| 186 | // ::new and ::delete aren't nested-name-specifiers. |
| 187 | tok::TokenKind NextKind = NextToken().getKind(); |
| 188 | if (NextKind == tok::kw_new || NextKind == tok::kw_delete) |
| 189 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 190 | |
David Majnemer | e8fb28f | 2014-12-29 19:19:18 +0000 | [diff] [blame] | 191 | if (NextKind == tok::l_brace) { |
| 192 | // It is invalid to have :: {, consume the scope qualifier and pretend |
| 193 | // like we never saw it. |
| 194 | Diag(ConsumeToken(), diag::err_expected) << tok::identifier; |
| 195 | } else { |
| 196 | // '::' - Global scope qualifier. |
| 197 | if (Actions.ActOnCXXGlobalScopeSpecifier(ConsumeToken(), SS)) |
| 198 | return true; |
Richard Trieu | 1f3ea7b | 2012-11-02 01:08:58 +0000 | [diff] [blame] | 199 | |
David Majnemer | e8fb28f | 2014-12-29 19:19:18 +0000 | [diff] [blame] | 200 | HasScopeSpecifier = true; |
| 201 | } |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 204 | if (Tok.is(tok::kw___super)) { |
| 205 | SourceLocation SuperLoc = ConsumeToken(); |
| 206 | if (!Tok.is(tok::coloncolon)) { |
| 207 | Diag(Tok.getLocation(), diag::err_expected_coloncolon_after_super); |
| 208 | return true; |
| 209 | } |
| 210 | |
| 211 | return Actions.ActOnSuperScopeSpecifier(SuperLoc, ConsumeToken(), SS); |
| 212 | } |
| 213 | |
Richard Smith | a9d1001 | 2014-10-04 01:57:39 +0000 | [diff] [blame] | 214 | if (!HasScopeSpecifier && |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 215 | Tok.isOneOf(tok::kw_decltype, tok::annot_decltype)) { |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 216 | DeclSpec DS(AttrFactory); |
| 217 | SourceLocation DeclLoc = Tok.getLocation(); |
| 218 | SourceLocation EndLoc = ParseDecltypeSpecifier(DS); |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 219 | |
| 220 | SourceLocation CCLoc; |
Richard Smith | 3f846bd | 2017-02-08 19:58:48 +0000 | [diff] [blame] | 221 | // Work around a standard defect: 'decltype(auto)::' is not a |
| 222 | // nested-name-specifier. |
| 223 | if (DS.getTypeSpecType() == DeclSpec::TST_decltype_auto || |
| 224 | !TryConsumeToken(tok::coloncolon, CCLoc)) { |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 225 | AnnotateExistingDecltypeSpecifier(DS, DeclLoc, EndLoc); |
| 226 | return false; |
| 227 | } |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 228 | |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 229 | if (Actions.ActOnCXXNestedNameSpecifierDecltype(SS, DS, CCLoc)) |
| 230 | SS.SetInvalid(SourceRange(DeclLoc, CCLoc)); |
| 231 | |
| 232 | HasScopeSpecifier = true; |
| 233 | } |
| 234 | |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 235 | while (true) { |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 236 | if (HasScopeSpecifier) { |
Ilya Biryukov | f1822ec | 2018-12-03 13:29:17 +0000 | [diff] [blame] | 237 | if (Tok.is(tok::code_completion)) { |
| 238 | // Code completion for a nested-name-specifier, where the code |
| 239 | // completion token follows the '::'. |
| 240 | Actions.CodeCompleteQualifiedId(getCurScope(), SS, EnteringContext, |
| 241 | ObjectType.get()); |
| 242 | // Include code completion token into the range of the scope otherwise |
| 243 | // when we try to annotate the scope tokens the dangling code completion |
| 244 | // token will cause assertion in |
| 245 | // Preprocessor::AnnotatePreviousCachedTokens. |
| 246 | SS.setEndLoc(Tok.getLocation()); |
| 247 | cutOffParsing(); |
| 248 | return true; |
| 249 | } |
| 250 | |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 251 | // C++ [basic.lookup.classref]p5: |
| 252 | // If the qualified-id has the form |
Douglas Gregor | 308047d | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 253 | // |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 254 | // ::class-name-or-namespace-name::... |
Douglas Gregor | 308047d | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 255 | // |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 256 | // the class-name-or-namespace-name is looked up in global scope as a |
| 257 | // class-name or namespace-name. |
| 258 | // |
| 259 | // To implement this, we clear out the object type as soon as we've |
| 260 | // seen a leading '::' or part of a nested-name-specifier. |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 261 | ObjectType = nullptr; |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 262 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 263 | |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 264 | // nested-name-specifier: |
Chris Lattner | 0eed3a6 | 2009-06-26 03:47:46 +0000 | [diff] [blame] | 265 | // nested-name-specifier 'template'[opt] simple-template-id '::' |
| 266 | |
| 267 | // Parse the optional 'template' keyword, then make sure we have |
| 268 | // 'identifier <' after it. |
| 269 | if (Tok.is(tok::kw_template)) { |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 270 | // If we don't have a scope specifier or an object type, this isn't a |
Eli Friedman | 2624be4 | 2009-08-29 04:08:08 +0000 | [diff] [blame] | 271 | // nested-name-specifier, since they aren't allowed to start with |
| 272 | // 'template'. |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 273 | if (!HasScopeSpecifier && !ObjectType) |
Eli Friedman | 2624be4 | 2009-08-29 04:08:08 +0000 | [diff] [blame] | 274 | break; |
| 275 | |
Douglas Gregor | 120635b | 2009-11-11 16:39:34 +0000 | [diff] [blame] | 276 | TentativeParsingAction TPA(*this); |
Chris Lattner | 0eed3a6 | 2009-06-26 03:47:46 +0000 | [diff] [blame] | 277 | SourceLocation TemplateKWLoc = ConsumeToken(); |
Richard Smith | d091dc1 | 2013-12-05 00:58:33 +0000 | [diff] [blame] | 278 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 279 | UnqualifiedId TemplateName; |
| 280 | if (Tok.is(tok::identifier)) { |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 281 | // Consume the identifier. |
Douglas Gregor | 120635b | 2009-11-11 16:39:34 +0000 | [diff] [blame] | 282 | TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 283 | ConsumeToken(); |
| 284 | } else if (Tok.is(tok::kw_operator)) { |
Richard Smith | d091dc1 | 2013-12-05 00:58:33 +0000 | [diff] [blame] | 285 | // We don't need to actually parse the unqualified-id in this case, |
| 286 | // because a simple-template-id cannot start with 'operator', but |
| 287 | // go ahead and parse it anyway for consistency with the case where |
| 288 | // we already annotated the template-id. |
| 289 | if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, |
Douglas Gregor | 120635b | 2009-11-11 16:39:34 +0000 | [diff] [blame] | 290 | TemplateName)) { |
| 291 | TPA.Commit(); |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 292 | break; |
Douglas Gregor | 120635b | 2009-11-11 16:39:34 +0000 | [diff] [blame] | 293 | } |
Richard Smith | d091dc1 | 2013-12-05 00:58:33 +0000 | [diff] [blame] | 294 | |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 295 | if (TemplateName.getKind() != UnqualifiedIdKind::IK_OperatorFunctionId && |
| 296 | TemplateName.getKind() != UnqualifiedIdKind::IK_LiteralOperatorId) { |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 297 | Diag(TemplateName.getSourceRange().getBegin(), |
| 298 | diag::err_id_after_template_in_nested_name_spec) |
| 299 | << TemplateName.getSourceRange(); |
Douglas Gregor | 120635b | 2009-11-11 16:39:34 +0000 | [diff] [blame] | 300 | TPA.Commit(); |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 301 | break; |
| 302 | } |
| 303 | } else { |
Douglas Gregor | 120635b | 2009-11-11 16:39:34 +0000 | [diff] [blame] | 304 | TPA.Revert(); |
Chris Lattner | 0eed3a6 | 2009-06-26 03:47:46 +0000 | [diff] [blame] | 305 | break; |
| 306 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 307 | |
Douglas Gregor | 120635b | 2009-11-11 16:39:34 +0000 | [diff] [blame] | 308 | // If the next token is not '<', we have a qualified-id that refers |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 309 | // to a template name, such as T::template apply, but is not a |
Douglas Gregor | 120635b | 2009-11-11 16:39:34 +0000 | [diff] [blame] | 310 | // template-id. |
| 311 | if (Tok.isNot(tok::less)) { |
| 312 | TPA.Revert(); |
| 313 | break; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Douglas Gregor | 120635b | 2009-11-11 16:39:34 +0000 | [diff] [blame] | 316 | // Commit to parsing the template-id. |
| 317 | TPA.Commit(); |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 318 | TemplateTy Template; |
Richard Smith | fd3dae0 | 2017-01-20 00:20:39 +0000 | [diff] [blame] | 319 | if (TemplateNameKind TNK = Actions.ActOnDependentTemplateName( |
| 320 | getCurScope(), SS, TemplateKWLoc, TemplateName, ObjectType, |
| 321 | EnteringContext, Template, /*AllowInjectedClassName*/ true)) { |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 322 | if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateKWLoc, |
| 323 | TemplateName, false)) |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 324 | return true; |
| 325 | } else |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 326 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 327 | |
Chris Lattner | 0eed3a6 | 2009-06-26 03:47:46 +0000 | [diff] [blame] | 328 | continue; |
| 329 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 330 | |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 331 | if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 332 | // We have |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 333 | // |
Richard Smith | 72bfbd8 | 2013-12-04 00:28:23 +0000 | [diff] [blame] | 334 | // template-id '::' |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 335 | // |
Richard Smith | 72bfbd8 | 2013-12-04 00:28:23 +0000 | [diff] [blame] | 336 | // So we need to check whether the template-id is a simple-template-id of |
| 337 | // the right kind (it should name a type or be dependent), and then |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 338 | // convert it into a type within the nested-name-specifier. |
Argyrios Kyrtzidis | c0c5dd2 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 339 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 340 | if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde)) { |
| 341 | *MayBePseudoDestructor = true; |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 342 | return false; |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Richard Smith | 7447af4 | 2013-03-26 01:15:19 +0000 | [diff] [blame] | 345 | if (LastII) |
| 346 | *LastII = TemplateId->Name; |
| 347 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 348 | // Consume the template-id token. |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 349 | ConsumeAnnotationToken(); |
Serge Pavlov | 6a7ffbe | 2014-04-13 16:52:03 +0000 | [diff] [blame] | 350 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 351 | assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!"); |
| 352 | SourceLocation CCLoc = ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 353 | |
David Blaikie | 8c045bc | 2011-11-07 03:30:03 +0000 | [diff] [blame] | 354 | HasScopeSpecifier = true; |
Serge Pavlov | 6a7ffbe | 2014-04-13 16:52:03 +0000 | [diff] [blame] | 355 | |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 356 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(), |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 357 | TemplateId->NumArgs); |
Serge Pavlov | 6a7ffbe | 2014-04-13 16:52:03 +0000 | [diff] [blame] | 358 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 359 | if (Actions.ActOnCXXNestedNameSpecifier(getCurScope(), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 360 | SS, |
| 361 | TemplateId->TemplateKWLoc, |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 362 | TemplateId->Template, |
| 363 | TemplateId->TemplateNameLoc, |
| 364 | TemplateId->LAngleLoc, |
| 365 | TemplateArgsPtr, |
| 366 | TemplateId->RAngleLoc, |
| 367 | CCLoc, |
| 368 | EnteringContext)) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 369 | SourceLocation StartLoc |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 370 | = SS.getBeginLoc().isValid()? SS.getBeginLoc() |
| 371 | : TemplateId->TemplateNameLoc; |
| 372 | SS.SetInvalid(SourceRange(StartLoc, CCLoc)); |
Chris Lattner | 704edfb | 2009-06-26 03:45:46 +0000 | [diff] [blame] | 373 | } |
Argyrios Kyrtzidis | 1393567 | 2011-05-03 18:45:38 +0000 | [diff] [blame] | 374 | |
Douglas Gregor | 8b6070b | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 375 | continue; |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Chris Lattner | e2355f7 | 2009-06-26 03:52:38 +0000 | [diff] [blame] | 378 | // The rest of the nested-name-specifier possibilities start with |
| 379 | // tok::identifier. |
| 380 | if (Tok.isNot(tok::identifier)) |
| 381 | break; |
| 382 | |
| 383 | IdentifierInfo &II = *Tok.getIdentifierInfo(); |
| 384 | |
| 385 | // nested-name-specifier: |
| 386 | // type-name '::' |
| 387 | // namespace-name '::' |
| 388 | // nested-name-specifier identifier '::' |
| 389 | Token Next = NextToken(); |
Serge Pavlov | d931b9f | 2016-08-08 04:02:15 +0000 | [diff] [blame] | 390 | Sema::NestedNameSpecInfo IdInfo(&II, Tok.getLocation(), Next.getLocation(), |
| 391 | ObjectType); |
| 392 | |
Chris Lattner | 1c42803 | 2009-12-07 01:36:53 +0000 | [diff] [blame] | 393 | // If we get foo:bar, this is almost certainly a typo for foo::bar. Recover |
| 394 | // and emit a fixit hint for it. |
Douglas Gregor | 90d554e | 2010-02-21 18:36:56 +0000 | [diff] [blame] | 395 | if (Next.is(tok::colon) && !ColonIsSacred) { |
Serge Pavlov | d931b9f | 2016-08-08 04:02:15 +0000 | [diff] [blame] | 396 | if (Actions.IsInvalidUnlessNestedName(getCurScope(), SS, IdInfo, |
Douglas Gregor | 90d554e | 2010-02-21 18:36:56 +0000 | [diff] [blame] | 397 | EnteringContext) && |
| 398 | // If the token after the colon isn't an identifier, it's still an |
| 399 | // error, but they probably meant something else strange so don't |
| 400 | // recover like this. |
| 401 | PP.LookAhead(1).is(tok::identifier)) { |
Serge Pavlov | 6a7ffbe | 2014-04-13 16:52:03 +0000 | [diff] [blame] | 402 | Diag(Next, diag::err_unexpected_colon_in_nested_name_spec) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 403 | << FixItHint::CreateReplacement(Next.getLocation(), "::"); |
Douglas Gregor | 90d554e | 2010-02-21 18:36:56 +0000 | [diff] [blame] | 404 | // Recover as if the user wrote '::'. |
| 405 | Next.setKind(tok::coloncolon); |
| 406 | } |
Chris Lattner | 1c42803 | 2009-12-07 01:36:53 +0000 | [diff] [blame] | 407 | } |
David Majnemer | f58efd9 | 2014-12-29 23:12:23 +0000 | [diff] [blame] | 408 | |
| 409 | if (Next.is(tok::coloncolon) && GetLookAheadToken(2).is(tok::l_brace)) { |
| 410 | // It is invalid to have :: {, consume the scope qualifier and pretend |
| 411 | // like we never saw it. |
| 412 | Token Identifier = Tok; // Stash away the identifier. |
| 413 | ConsumeToken(); // Eat the identifier, current token is now '::'. |
David Majnemer | ec3f49d | 2014-12-29 23:24:27 +0000 | [diff] [blame] | 414 | Diag(PP.getLocForEndOfToken(ConsumeToken()), diag::err_expected) |
| 415 | << tok::identifier; |
David Majnemer | f58efd9 | 2014-12-29 23:12:23 +0000 | [diff] [blame] | 416 | UnconsumeToken(Identifier); // Stick the identifier back. |
| 417 | Next = NextToken(); // Point Next at the '{' token. |
| 418 | } |
| 419 | |
Chris Lattner | e2355f7 | 2009-06-26 03:52:38 +0000 | [diff] [blame] | 420 | if (Next.is(tok::coloncolon)) { |
Douglas Gregor | 0d5b0a1 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 421 | if (CheckForDestructor && GetLookAheadToken(2).is(tok::tilde) && |
Serge Pavlov | d931b9f | 2016-08-08 04:02:15 +0000 | [diff] [blame] | 422 | !Actions.isNonTypeNestedNameSpecifier(getCurScope(), SS, IdInfo)) { |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 423 | *MayBePseudoDestructor = true; |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 424 | return false; |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Serge Pavlov | 6a7ffbe | 2014-04-13 16:52:03 +0000 | [diff] [blame] | 427 | if (ColonIsSacred) { |
| 428 | const Token &Next2 = GetLookAheadToken(2); |
| 429 | if (Next2.is(tok::kw_private) || Next2.is(tok::kw_protected) || |
| 430 | Next2.is(tok::kw_public) || Next2.is(tok::kw_virtual)) { |
| 431 | Diag(Next2, diag::err_unexpected_token_in_nested_name_spec) |
| 432 | << Next2.getName() |
| 433 | << FixItHint::CreateReplacement(Next.getLocation(), ":"); |
| 434 | Token ColonColon; |
| 435 | PP.Lex(ColonColon); |
| 436 | ColonColon.setKind(tok::colon); |
Ilya Biryukov | 929af67 | 2019-05-17 09:32:05 +0000 | [diff] [blame^] | 437 | PP.EnterToken(ColonColon, /*IsReinject*/ true); |
Serge Pavlov | 6a7ffbe | 2014-04-13 16:52:03 +0000 | [diff] [blame] | 438 | break; |
| 439 | } |
| 440 | } |
| 441 | |
Richard Smith | 7447af4 | 2013-03-26 01:15:19 +0000 | [diff] [blame] | 442 | if (LastII) |
| 443 | *LastII = &II; |
| 444 | |
Chris Lattner | e2355f7 | 2009-06-26 03:52:38 +0000 | [diff] [blame] | 445 | // We have an identifier followed by a '::'. Lookup this name |
| 446 | // as the name in a nested-name-specifier. |
Serge Pavlov | 6a7ffbe | 2014-04-13 16:52:03 +0000 | [diff] [blame] | 447 | Token Identifier = Tok; |
Chris Lattner | e2355f7 | 2009-06-26 03:52:38 +0000 | [diff] [blame] | 448 | SourceLocation IdLoc = ConsumeToken(); |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 449 | assert(Tok.isOneOf(tok::coloncolon, tok::colon) && |
Chris Lattner | 1c42803 | 2009-12-07 01:36:53 +0000 | [diff] [blame] | 450 | "NextToken() not working properly!"); |
Serge Pavlov | 6a7ffbe | 2014-04-13 16:52:03 +0000 | [diff] [blame] | 451 | Token ColonColon = Tok; |
Chris Lattner | e2355f7 | 2009-06-26 03:52:38 +0000 | [diff] [blame] | 452 | SourceLocation CCLoc = ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 453 | |
Serge Pavlov | 6a7ffbe | 2014-04-13 16:52:03 +0000 | [diff] [blame] | 454 | bool IsCorrectedToColon = false; |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 455 | bool *CorrectionFlagPtr = ColonIsSacred ? &IsCorrectedToColon : nullptr; |
Matthias Gehre | dc01bb4 | 2017-03-17 21:41:20 +0000 | [diff] [blame] | 456 | if (Actions.ActOnCXXNestedNameSpecifier( |
| 457 | getCurScope(), IdInfo, EnteringContext, SS, false, |
| 458 | CorrectionFlagPtr, OnlyNamespace)) { |
Serge Pavlov | 6a7ffbe | 2014-04-13 16:52:03 +0000 | [diff] [blame] | 459 | // Identifier is not recognized as a nested name, but we can have |
| 460 | // mistyped '::' instead of ':'. |
| 461 | if (CorrectionFlagPtr && IsCorrectedToColon) { |
| 462 | ColonColon.setKind(tok::colon); |
Ilya Biryukov | 929af67 | 2019-05-17 09:32:05 +0000 | [diff] [blame^] | 463 | PP.EnterToken(Tok, /*IsReinject*/ true); |
| 464 | PP.EnterToken(ColonColon, /*IsReinject*/ true); |
Serge Pavlov | 6a7ffbe | 2014-04-13 16:52:03 +0000 | [diff] [blame] | 465 | Tok = Identifier; |
| 466 | break; |
| 467 | } |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 468 | SS.SetInvalid(SourceRange(IdLoc, CCLoc)); |
Serge Pavlov | 6a7ffbe | 2014-04-13 16:52:03 +0000 | [diff] [blame] | 469 | } |
| 470 | HasScopeSpecifier = true; |
Chris Lattner | e2355f7 | 2009-06-26 03:52:38 +0000 | [diff] [blame] | 471 | continue; |
| 472 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 473 | |
Richard Trieu | 01fc001 | 2011-09-19 19:01:00 +0000 | [diff] [blame] | 474 | CheckForTemplateAndDigraph(Next, ObjectType, EnteringContext, II, SS); |
Richard Smith | 5585849 | 2011-04-14 21:45:45 +0000 | [diff] [blame] | 475 | |
Chris Lattner | e2355f7 | 2009-06-26 03:52:38 +0000 | [diff] [blame] | 476 | // nested-name-specifier: |
| 477 | // type-name '<' |
| 478 | if (Next.is(tok::less)) { |
| 479 | TemplateTy Template; |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 480 | UnqualifiedId TemplateName; |
| 481 | TemplateName.setIdentifier(&II, Tok.getLocation()); |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 482 | bool MemberOfUnknownSpecialization; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 483 | if (TemplateNameKind TNK = Actions.isTemplateName(getCurScope(), SS, |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 484 | /*hasTemplateKeyword=*/false, |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 485 | TemplateName, |
Douglas Gregor | b7bfe79 | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 486 | ObjectType, |
Douglas Gregor | e861bac | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 487 | EnteringContext, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 488 | Template, |
| 489 | MemberOfUnknownSpecialization)) { |
Richard Smith | b23c5e8 | 2019-05-09 03:31:27 +0000 | [diff] [blame] | 490 | // If lookup didn't find anything, we treat the name as a template-name |
| 491 | // anyway. C++20 requires this, and in prior language modes it improves |
| 492 | // error recovery. But before we commit to this, check that we actually |
| 493 | // have something that looks like a template-argument-list next. |
| 494 | if (!IsTypename && TNK == TNK_Undeclared_template && |
| 495 | isTemplateArgumentList(1) == TPResult::False) |
| 496 | break; |
| 497 | |
David Blaikie | 8c045bc | 2011-11-07 03:30:03 +0000 | [diff] [blame] | 498 | // We have found a template name, so annotate this token |
Chris Lattner | e2355f7 | 2009-06-26 03:52:38 +0000 | [diff] [blame] | 499 | // with a template-id annotation. We do not permit the |
| 500 | // template-id to be translated into a type annotation, |
| 501 | // because some clients (e.g., the parsing of class template |
| 502 | // specializations) still want to see the original template-id |
| 503 | // token. |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 504 | ConsumeToken(); |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 505 | if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(), |
| 506 | TemplateName, false)) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 507 | return true; |
Chris Lattner | e2355f7 | 2009-06-26 03:52:38 +0000 | [diff] [blame] | 508 | continue; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 511 | if (MemberOfUnknownSpecialization && (ObjectType || SS.isSet()) && |
Richard Smith | b23c5e8 | 2019-05-09 03:31:27 +0000 | [diff] [blame] | 512 | (IsTypename || isTemplateArgumentList(1) == TPResult::True)) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 513 | // We have something like t::getAs<T>, where getAs is a |
Douglas Gregor | 20c38a7 | 2010-05-21 23:43:39 +0000 | [diff] [blame] | 514 | // member of an unknown specialization. However, this will only |
| 515 | // parse correctly as a template, so suggest the keyword 'template' |
| 516 | // before 'getAs' and treat this as a dependent template name. |
Francois Pichet | 4e7a2c0 | 2011-03-27 19:41:34 +0000 | [diff] [blame] | 517 | unsigned DiagID = diag::err_missing_dependent_template_keyword; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 518 | if (getLangOpts().MicrosoftExt) |
Francois Pichet | 9392165 | 2011-04-22 08:25:24 +0000 | [diff] [blame] | 519 | DiagID = diag::warn_missing_dependent_template_keyword; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 520 | |
Francois Pichet | 4e7a2c0 | 2011-03-27 19:41:34 +0000 | [diff] [blame] | 521 | Diag(Tok.getLocation(), DiagID) |
Douglas Gregor | 20c38a7 | 2010-05-21 23:43:39 +0000 | [diff] [blame] | 522 | << II.getName() |
| 523 | << FixItHint::CreateInsertion(Tok.getLocation(), "template "); |
Richard Smith | fd3dae0 | 2017-01-20 00:20:39 +0000 | [diff] [blame] | 524 | |
| 525 | if (TemplateNameKind TNK = Actions.ActOnDependentTemplateName( |
Richard Smith | 7981004 | 2018-05-11 02:43:08 +0000 | [diff] [blame] | 526 | getCurScope(), SS, Tok.getLocation(), TemplateName, ObjectType, |
Richard Smith | fd3dae0 | 2017-01-20 00:20:39 +0000 | [diff] [blame] | 527 | EnteringContext, Template, /*AllowInjectedClassName*/ true)) { |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 528 | // Consume the identifier. |
| 529 | ConsumeToken(); |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 530 | if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(), |
| 531 | TemplateName, false)) |
| 532 | return true; |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 533 | } |
| 534 | else |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 535 | return true; |
| 536 | |
| 537 | continue; |
Chris Lattner | e2355f7 | 2009-06-26 03:52:38 +0000 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 541 | // We don't have any tokens that form the beginning of a |
| 542 | // nested-name-specifier, so we're done. |
| 543 | break; |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 544 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 545 | |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 546 | // Even if we didn't see any pieces of a nested-name-specifier, we |
| 547 | // still check whether there is a tilde in this position, which |
| 548 | // indicates a potential pseudo-destructor. |
| 549 | if (CheckForDestructor && Tok.is(tok::tilde)) |
| 550 | *MayBePseudoDestructor = true; |
| 551 | |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 552 | return false; |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Kaelyn Takata | b16e632 | 2014-11-20 22:06:40 +0000 | [diff] [blame] | 555 | ExprResult Parser::tryParseCXXIdExpression(CXXScopeSpec &SS, bool isAddressOfOperand, |
| 556 | Token &Replacement) { |
| 557 | SourceLocation TemplateKWLoc; |
| 558 | UnqualifiedId Name; |
| 559 | if (ParseUnqualifiedId(SS, |
| 560 | /*EnteringContext=*/false, |
| 561 | /*AllowDestructorName=*/false, |
| 562 | /*AllowConstructorName=*/false, |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 563 | /*AllowDeductionGuide=*/false, |
Richard Smith | c08b693 | 2018-04-27 02:00:13 +0000 | [diff] [blame] | 564 | /*ObjectType=*/nullptr, &TemplateKWLoc, Name)) |
Kaelyn Takata | b16e632 | 2014-11-20 22:06:40 +0000 | [diff] [blame] | 565 | return ExprError(); |
| 566 | |
| 567 | // This is only the direct operand of an & operator if it is not |
| 568 | // followed by a postfix-expression suffix. |
| 569 | if (isAddressOfOperand && isPostfixExpressionSuffixStart()) |
| 570 | isAddressOfOperand = false; |
| 571 | |
Richard Smith | c2dead4 | 2018-06-27 01:32:04 +0000 | [diff] [blame] | 572 | ExprResult E = Actions.ActOnIdExpression( |
| 573 | getCurScope(), SS, TemplateKWLoc, Name, Tok.is(tok::l_paren), |
Bruno Ricci | 70ad396 | 2019-03-25 17:08:51 +0000 | [diff] [blame] | 574 | isAddressOfOperand, /*CCC=*/nullptr, /*IsInlineAsmIdentifier=*/false, |
Richard Smith | c2dead4 | 2018-06-27 01:32:04 +0000 | [diff] [blame] | 575 | &Replacement); |
| 576 | if (!E.isInvalid() && !E.isUnset() && Tok.is(tok::less)) |
| 577 | checkPotentialAngleBracket(E); |
| 578 | return E; |
Kaelyn Takata | b16e632 | 2014-11-20 22:06:40 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 581 | /// ParseCXXIdExpression - Handle id-expression. |
| 582 | /// |
| 583 | /// id-expression: |
| 584 | /// unqualified-id |
| 585 | /// qualified-id |
| 586 | /// |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 587 | /// qualified-id: |
| 588 | /// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id |
| 589 | /// '::' identifier |
| 590 | /// '::' operator-function-id |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 591 | /// '::' template-id |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 592 | /// |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 593 | /// NOTE: The standard specifies that, for qualified-id, the parser does not |
| 594 | /// expect: |
| 595 | /// |
| 596 | /// '::' conversion-function-id |
| 597 | /// '::' '~' class-name |
| 598 | /// |
| 599 | /// This may cause a slight inconsistency on diagnostics: |
| 600 | /// |
| 601 | /// class C {}; |
| 602 | /// namespace A {} |
| 603 | /// void f() { |
| 604 | /// :: A :: ~ C(); // Some Sema error about using destructor with a |
| 605 | /// // namespace. |
| 606 | /// :: ~ C(); // Some Parser error like 'unexpected ~'. |
| 607 | /// } |
| 608 | /// |
| 609 | /// We simplify the parser a bit and make it work like: |
| 610 | /// |
| 611 | /// qualified-id: |
| 612 | /// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id |
| 613 | /// '::' unqualified-id |
| 614 | /// |
| 615 | /// That way Sema can handle and report similar errors for namespaces and the |
| 616 | /// global scope. |
| 617 | /// |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 618 | /// The isAddressOfOperand parameter indicates that this id-expression is a |
| 619 | /// direct operand of the address-of operator. This is, besides member contexts, |
| 620 | /// the only place where a qualified-id naming a non-static class member may |
| 621 | /// appear. |
| 622 | /// |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 623 | ExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) { |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 624 | // qualified-id: |
| 625 | // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id |
| 626 | // '::' unqualified-id |
| 627 | // |
| 628 | CXXScopeSpec SS; |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 629 | ParseOptionalCXXScopeSpecifier(SS, nullptr, /*EnteringContext=*/false); |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 630 | |
Kaelyn Takata | b16e632 | 2014-11-20 22:06:40 +0000 | [diff] [blame] | 631 | Token Replacement; |
Nico Weber | 01a46ad | 2015-02-15 06:15:40 +0000 | [diff] [blame] | 632 | ExprResult Result = |
| 633 | tryParseCXXIdExpression(SS, isAddressOfOperand, Replacement); |
Kaelyn Takata | b16e632 | 2014-11-20 22:06:40 +0000 | [diff] [blame] | 634 | if (Result.isUnset()) { |
| 635 | // If the ExprResult is valid but null, then typo correction suggested a |
| 636 | // keyword replacement that needs to be reparsed. |
| 637 | UnconsumeToken(Replacement); |
| 638 | Result = tryParseCXXIdExpression(SS, isAddressOfOperand, Replacement); |
| 639 | } |
| 640 | assert(!Result.isUnset() && "Typo correction suggested a keyword replacement " |
| 641 | "for a previous keyword suggestion"); |
| 642 | return Result; |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Richard Smith | 21b3ab4 | 2013-05-09 21:36:41 +0000 | [diff] [blame] | 645 | /// ParseLambdaExpression - Parse a C++11 lambda expression. |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 646 | /// |
| 647 | /// lambda-expression: |
| 648 | /// lambda-introducer lambda-declarator[opt] compound-statement |
Hamza Sood | 8205a81 | 2019-05-04 10:49:46 +0000 | [diff] [blame] | 649 | /// lambda-introducer '<' template-parameter-list '>' |
| 650 | /// lambda-declarator[opt] compound-statement |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 651 | /// |
| 652 | /// lambda-introducer: |
| 653 | /// '[' lambda-capture[opt] ']' |
| 654 | /// |
| 655 | /// lambda-capture: |
| 656 | /// capture-default |
| 657 | /// capture-list |
| 658 | /// capture-default ',' capture-list |
| 659 | /// |
| 660 | /// capture-default: |
| 661 | /// '&' |
| 662 | /// '=' |
| 663 | /// |
| 664 | /// capture-list: |
| 665 | /// capture |
| 666 | /// capture-list ',' capture |
| 667 | /// |
| 668 | /// capture: |
Richard Smith | 21b3ab4 | 2013-05-09 21:36:41 +0000 | [diff] [blame] | 669 | /// simple-capture |
| 670 | /// init-capture [C++1y] |
| 671 | /// |
| 672 | /// simple-capture: |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 673 | /// identifier |
| 674 | /// '&' identifier |
| 675 | /// 'this' |
| 676 | /// |
Richard Smith | 21b3ab4 | 2013-05-09 21:36:41 +0000 | [diff] [blame] | 677 | /// init-capture: [C++1y] |
| 678 | /// identifier initializer |
| 679 | /// '&' identifier initializer |
| 680 | /// |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 681 | /// lambda-declarator: |
| 682 | /// '(' parameter-declaration-clause ')' attribute-specifier[opt] |
| 683 | /// 'mutable'[opt] exception-specification[opt] |
| 684 | /// trailing-return-type[opt] |
| 685 | /// |
| 686 | ExprResult Parser::ParseLambdaExpression() { |
| 687 | // Parse lambda-introducer. |
| 688 | LambdaIntroducer Intro; |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 689 | Optional<unsigned> DiagID = ParseLambdaIntroducer(Intro); |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 690 | if (DiagID) { |
| 691 | Diag(Tok, DiagID.getValue()); |
David Majnemer | 234b818 | 2015-01-12 03:36:37 +0000 | [diff] [blame] | 692 | SkipUntil(tok::r_square, StopAtSemi); |
| 693 | SkipUntil(tok::l_brace, StopAtSemi); |
| 694 | SkipUntil(tok::r_brace, StopAtSemi); |
Eli Friedman | c7c9714 | 2012-01-04 02:40:39 +0000 | [diff] [blame] | 695 | return ExprError(); |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | return ParseLambdaExpressionAfterIntroducer(Intro); |
| 699 | } |
| 700 | |
| 701 | /// TryParseLambdaExpression - Use lookahead and potentially tentative |
| 702 | /// parsing to determine if we are looking at a C++0x lambda expression, and parse |
| 703 | /// it if we are. |
| 704 | /// |
| 705 | /// If we are not looking at a lambda expression, returns ExprError(). |
| 706 | ExprResult Parser::TryParseLambdaExpression() { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 707 | assert(getLangOpts().CPlusPlus11 |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 708 | && Tok.is(tok::l_square) |
| 709 | && "Not at the start of a possible lambda expression."); |
| 710 | |
Bruno Cardoso Lopes | 29b3423 | 2016-05-31 18:46:31 +0000 | [diff] [blame] | 711 | const Token Next = NextToken(); |
| 712 | if (Next.is(tok::eof)) // Nothing else to lookup here... |
| 713 | return ExprEmpty(); |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 714 | |
Bruno Cardoso Lopes | 29b3423 | 2016-05-31 18:46:31 +0000 | [diff] [blame] | 715 | const Token After = GetLookAheadToken(2); |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 716 | // If lookahead indicates this is a lambda... |
| 717 | if (Next.is(tok::r_square) || // [] |
| 718 | Next.is(tok::equal) || // [= |
| 719 | (Next.is(tok::amp) && // [&] or [&, |
| 720 | (After.is(tok::r_square) || |
| 721 | After.is(tok::comma))) || |
| 722 | (Next.is(tok::identifier) && // [identifier] |
| 723 | After.is(tok::r_square))) { |
| 724 | return ParseLambdaExpression(); |
| 725 | } |
| 726 | |
Eli Friedman | c7c9714 | 2012-01-04 02:40:39 +0000 | [diff] [blame] | 727 | // If lookahead indicates an ObjC message send... |
| 728 | // [identifier identifier |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 729 | if (Next.is(tok::identifier) && After.is(tok::identifier)) { |
Eli Friedman | c7c9714 | 2012-01-04 02:40:39 +0000 | [diff] [blame] | 730 | return ExprEmpty(); |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 731 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 732 | |
Eli Friedman | c7c9714 | 2012-01-04 02:40:39 +0000 | [diff] [blame] | 733 | // Here, we're stuck: lambda introducers and Objective-C message sends are |
| 734 | // unambiguous, but it requires arbitrary lookhead. [a,b,c,d,e,f,g] is a |
| 735 | // lambda, and [a,b,c,d,e,f,g h] is a Objective-C message send. Instead of |
| 736 | // writing two routines to parse a lambda introducer, just try to parse |
| 737 | // a lambda introducer first, and fall back if that fails. |
| 738 | // (TryParseLambdaIntroducer never produces any diagnostic output.) |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 739 | LambdaIntroducer Intro; |
| 740 | if (TryParseLambdaIntroducer(Intro)) |
Eli Friedman | c7c9714 | 2012-01-04 02:40:39 +0000 | [diff] [blame] | 741 | return ExprEmpty(); |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 742 | |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 743 | return ParseLambdaExpressionAfterIntroducer(Intro); |
| 744 | } |
| 745 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 746 | /// Parse a lambda introducer. |
Richard Smith | f44d2a8 | 2013-05-21 22:21:19 +0000 | [diff] [blame] | 747 | /// \param Intro A LambdaIntroducer filled in with information about the |
| 748 | /// contents of the lambda-introducer. |
| 749 | /// \param SkippedInits If non-null, we are disambiguating between an Obj-C |
| 750 | /// message send and a lambda expression. In this mode, we will |
| 751 | /// sometimes skip the initializers for init-captures and not fully |
| 752 | /// populate \p Intro. This flag will be set to \c true if we do so. |
| 753 | /// \return A DiagnosticID if it hit something unexpected. The location for |
Malcolm Parsons | ffd21d3 | 2017-01-11 11:23:22 +0000 | [diff] [blame] | 754 | /// the diagnostic is that of the current token. |
Richard Smith | f44d2a8 | 2013-05-21 22:21:19 +0000 | [diff] [blame] | 755 | Optional<unsigned> Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro, |
| 756 | bool *SkippedInits) { |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 757 | typedef Optional<unsigned> DiagResult; |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 758 | |
| 759 | assert(Tok.is(tok::l_square) && "Lambda expressions begin with '['."); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 760 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 761 | T.consumeOpen(); |
| 762 | |
| 763 | Intro.Range.setBegin(T.getOpenLocation()); |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 764 | |
| 765 | bool first = true; |
| 766 | |
| 767 | // Parse capture-default. |
| 768 | if (Tok.is(tok::amp) && |
| 769 | (NextToken().is(tok::comma) || NextToken().is(tok::r_square))) { |
| 770 | Intro.Default = LCD_ByRef; |
Douglas Gregor | a1bffa2 | 2012-02-10 17:46:20 +0000 | [diff] [blame] | 771 | Intro.DefaultLoc = ConsumeToken(); |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 772 | first = false; |
| 773 | } else if (Tok.is(tok::equal)) { |
| 774 | Intro.Default = LCD_ByCopy; |
Douglas Gregor | a1bffa2 | 2012-02-10 17:46:20 +0000 | [diff] [blame] | 775 | Intro.DefaultLoc = ConsumeToken(); |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 776 | first = false; |
| 777 | } |
| 778 | |
| 779 | while (Tok.isNot(tok::r_square)) { |
| 780 | if (!first) { |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 781 | if (Tok.isNot(tok::comma)) { |
Douglas Gregor | 721b14d | 2012-07-31 00:50:07 +0000 | [diff] [blame] | 782 | // Provide a completion for a lambda introducer here. Except |
| 783 | // in Objective-C, where this is Almost Surely meant to be a message |
| 784 | // send. In that case, fail here and let the ObjC message |
| 785 | // expression parser perform the completion. |
Douglas Gregor | 2d8db8f | 2012-07-31 15:27:48 +0000 | [diff] [blame] | 786 | if (Tok.is(tok::code_completion) && |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 787 | !(getLangOpts().ObjC && Intro.Default == LCD_None && |
Douglas Gregor | 2d8db8f | 2012-07-31 15:27:48 +0000 | [diff] [blame] | 788 | !Intro.Captures.empty())) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 789 | Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro, |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 790 | /*AfterAmpersand=*/false); |
Alp Toker | 1c583cc | 2014-05-02 03:43:14 +0000 | [diff] [blame] | 791 | cutOffParsing(); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 792 | break; |
| 793 | } |
| 794 | |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 795 | return DiagResult(diag::err_expected_comma_or_rsquare); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 796 | } |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 797 | ConsumeToken(); |
| 798 | } |
| 799 | |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 800 | if (Tok.is(tok::code_completion)) { |
| 801 | // If we're in Objective-C++ and we have a bare '[', then this is more |
| 802 | // likely to be a message receiver. |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 803 | if (getLangOpts().ObjC && first) |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 804 | Actions.CodeCompleteObjCMessageReceiver(getCurScope()); |
| 805 | else |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 806 | Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro, |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 807 | /*AfterAmpersand=*/false); |
Alp Toker | 1c583cc | 2014-05-02 03:43:14 +0000 | [diff] [blame] | 808 | cutOffParsing(); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 809 | break; |
| 810 | } |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 811 | |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 812 | first = false; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 813 | |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 814 | // Parse capture. |
| 815 | LambdaCaptureKind Kind = LCK_ByCopy; |
Richard Smith | 42b1057 | 2015-11-11 01:36:17 +0000 | [diff] [blame] | 816 | LambdaCaptureInitKind InitKind = LambdaCaptureInitKind::NoInit; |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 817 | SourceLocation Loc; |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 818 | IdentifierInfo *Id = nullptr; |
Douglas Gregor | 3e308b1 | 2012-02-14 19:27:52 +0000 | [diff] [blame] | 819 | SourceLocation EllipsisLoc; |
Richard Smith | 21b3ab4 | 2013-05-09 21:36:41 +0000 | [diff] [blame] | 820 | ExprResult Init; |
Alexander Shaposhnikov | 832f49b | 2018-07-16 07:23:47 +0000 | [diff] [blame] | 821 | SourceLocation LocStart = Tok.getLocation(); |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 822 | |
| 823 | if (Tok.is(tok::star)) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 824 | Loc = ConsumeToken(); |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 825 | if (Tok.is(tok::kw_this)) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 826 | ConsumeToken(); |
| 827 | Kind = LCK_StarThis; |
Faisal Vali | dc6b596 | 2016-03-21 09:25:37 +0000 | [diff] [blame] | 828 | } else { |
| 829 | return DiagResult(diag::err_expected_star_this_capture); |
| 830 | } |
| 831 | } else if (Tok.is(tok::kw_this)) { |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 832 | Kind = LCK_This; |
| 833 | Loc = ConsumeToken(); |
| 834 | } else { |
| 835 | if (Tok.is(tok::amp)) { |
| 836 | Kind = LCK_ByRef; |
| 837 | ConsumeToken(); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 838 | |
| 839 | if (Tok.is(tok::code_completion)) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 840 | Actions.CodeCompleteLambdaIntroducer(getCurScope(), Intro, |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 841 | /*AfterAmpersand=*/true); |
Alp Toker | 1c583cc | 2014-05-02 03:43:14 +0000 | [diff] [blame] | 842 | cutOffParsing(); |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 843 | break; |
| 844 | } |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | if (Tok.is(tok::identifier)) { |
| 848 | Id = Tok.getIdentifierInfo(); |
| 849 | Loc = ConsumeToken(); |
| 850 | } else if (Tok.is(tok::kw_this)) { |
| 851 | // FIXME: If we want to suggest a fixit here, will need to return more |
| 852 | // than just DiagnosticID. Perhaps full DiagnosticBuilder that can be |
| 853 | // Clear()ed to prevent emission in case of tentative parsing? |
| 854 | return DiagResult(diag::err_this_captured_by_reference); |
| 855 | } else { |
| 856 | return DiagResult(diag::err_expected_capture); |
| 857 | } |
Richard Smith | 21b3ab4 | 2013-05-09 21:36:41 +0000 | [diff] [blame] | 858 | |
| 859 | if (Tok.is(tok::l_paren)) { |
| 860 | BalancedDelimiterTracker Parens(*this, tok::l_paren); |
| 861 | Parens.consumeOpen(); |
| 862 | |
Richard Smith | 42b1057 | 2015-11-11 01:36:17 +0000 | [diff] [blame] | 863 | InitKind = LambdaCaptureInitKind::DirectInit; |
| 864 | |
Richard Smith | 21b3ab4 | 2013-05-09 21:36:41 +0000 | [diff] [blame] | 865 | ExprVector Exprs; |
| 866 | CommaLocsTy Commas; |
Richard Smith | f44d2a8 | 2013-05-21 22:21:19 +0000 | [diff] [blame] | 867 | if (SkippedInits) { |
| 868 | Parens.skipToEnd(); |
| 869 | *SkippedInits = true; |
| 870 | } else if (ParseExpressionList(Exprs, Commas)) { |
Richard Smith | 21b3ab4 | 2013-05-09 21:36:41 +0000 | [diff] [blame] | 871 | Parens.skipToEnd(); |
| 872 | Init = ExprError(); |
| 873 | } else { |
| 874 | Parens.consumeClose(); |
| 875 | Init = Actions.ActOnParenListExpr(Parens.getOpenLocation(), |
| 876 | Parens.getCloseLocation(), |
| 877 | Exprs); |
| 878 | } |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 879 | } else if (Tok.isOneOf(tok::l_brace, tok::equal)) { |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 880 | // Each lambda init-capture forms its own full expression, which clears |
| 881 | // Actions.MaybeODRUseExprs. So create an expression evaluation context |
| 882 | // to save the necessary state, and restore it later. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 883 | EnterExpressionEvaluationContext EC( |
| 884 | Actions, Sema::ExpressionEvaluationContext::PotentiallyEvaluated); |
Richard Smith | 42b1057 | 2015-11-11 01:36:17 +0000 | [diff] [blame] | 885 | |
| 886 | if (TryConsumeToken(tok::equal)) |
| 887 | InitKind = LambdaCaptureInitKind::CopyInit; |
| 888 | else |
| 889 | InitKind = LambdaCaptureInitKind::ListInit; |
Richard Smith | 21b3ab4 | 2013-05-09 21:36:41 +0000 | [diff] [blame] | 890 | |
Richard Smith | 215f423 | 2015-02-11 02:41:33 +0000 | [diff] [blame] | 891 | if (!SkippedInits) { |
Richard Smith | f44d2a8 | 2013-05-21 22:21:19 +0000 | [diff] [blame] | 892 | Init = ParseInitializer(); |
Richard Smith | 215f423 | 2015-02-11 02:41:33 +0000 | [diff] [blame] | 893 | } else if (Tok.is(tok::l_brace)) { |
Richard Smith | f44d2a8 | 2013-05-21 22:21:19 +0000 | [diff] [blame] | 894 | BalancedDelimiterTracker Braces(*this, tok::l_brace); |
| 895 | Braces.consumeOpen(); |
| 896 | Braces.skipToEnd(); |
| 897 | *SkippedInits = true; |
| 898 | } else { |
| 899 | // We're disambiguating this: |
| 900 | // |
| 901 | // [..., x = expr |
| 902 | // |
| 903 | // We need to find the end of the following expression in order to |
Richard Smith | 9e2f0a4 | 2014-04-13 04:31:48 +0000 | [diff] [blame] | 904 | // determine whether this is an Obj-C message send's receiver, a |
| 905 | // C99 designator, or a lambda init-capture. |
Richard Smith | f44d2a8 | 2013-05-21 22:21:19 +0000 | [diff] [blame] | 906 | // |
| 907 | // Parse the expression to find where it ends, and annotate it back |
| 908 | // onto the tokens. We would have parsed this expression the same way |
| 909 | // in either case: both the RHS of an init-capture and the RHS of an |
| 910 | // assignment expression are parsed as an initializer-clause, and in |
| 911 | // neither case can anything be added to the scope between the '[' and |
| 912 | // here. |
| 913 | // |
| 914 | // FIXME: This is horrible. Adding a mechanism to skip an expression |
| 915 | // would be much cleaner. |
| 916 | // FIXME: If there is a ',' before the next ']' or ':', we can skip to |
| 917 | // that instead. (And if we see a ':' with no matching '?', we can |
| 918 | // classify this as an Obj-C message send.) |
| 919 | SourceLocation StartLoc = Tok.getLocation(); |
| 920 | InMessageExpressionRAIIObject MaybeInMessageExpression(*this, true); |
| 921 | Init = ParseInitializer(); |
Akira Hatanaka | 51e60f9 | 2016-12-20 02:11:29 +0000 | [diff] [blame] | 922 | if (!Init.isInvalid()) |
| 923 | Init = Actions.CorrectDelayedTyposInExpr(Init.get()); |
Richard Smith | f44d2a8 | 2013-05-21 22:21:19 +0000 | [diff] [blame] | 924 | |
| 925 | if (Tok.getLocation() != StartLoc) { |
| 926 | // Back out the lexing of the token after the initializer. |
| 927 | PP.RevertCachedTokens(1); |
| 928 | |
| 929 | // Replace the consumed tokens with an appropriate annotation. |
| 930 | Tok.setLocation(StartLoc); |
| 931 | Tok.setKind(tok::annot_primary_expr); |
| 932 | setExprAnnotation(Tok, Init); |
| 933 | Tok.setAnnotationEndLoc(PP.getLastCachedTokenLocation()); |
| 934 | PP.AnnotateCachedTokens(Tok); |
| 935 | |
| 936 | // Consume the annotated initializer. |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 937 | ConsumeAnnotationToken(); |
Richard Smith | f44d2a8 | 2013-05-21 22:21:19 +0000 | [diff] [blame] | 938 | } |
| 939 | } |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 940 | } else |
| 941 | TryConsumeToken(tok::ellipsis, EllipsisLoc); |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 942 | } |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 943 | // If this is an init capture, process the initialization expression |
| 944 | // right away. For lambda init-captures such as the following: |
| 945 | // const int x = 10; |
| 946 | // auto L = [i = x+1](int a) { |
| 947 | // return [j = x+2, |
| 948 | // &k = x](char b) { }; |
| 949 | // }; |
| 950 | // keep in mind that each lambda init-capture has to have: |
| 951 | // - its initialization expression executed in the context |
| 952 | // of the enclosing/parent decl-context. |
| 953 | // - but the variable itself has to be 'injected' into the |
| 954 | // decl-context of its lambda's call-operator (which has |
| 955 | // not yet been created). |
| 956 | // Each init-expression is a full-expression that has to get |
| 957 | // Sema-analyzed (for capturing etc.) before its lambda's |
| 958 | // call-operator's decl-context, scope & scopeinfo are pushed on their |
| 959 | // respective stacks. Thus if any variable is odr-used in the init-capture |
| 960 | // it will correctly get captured in the enclosing lambda, if one exists. |
| 961 | // The init-variables above are created later once the lambdascope and |
| 962 | // call-operators decl-context is pushed onto its respective stack. |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 963 | |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 964 | // Since the lambda init-capture's initializer expression occurs in the |
| 965 | // context of the enclosing function or lambda, therefore we can not wait |
| 966 | // till a lambda scope has been pushed on before deciding whether the |
| 967 | // variable needs to be captured. We also need to process all |
| 968 | // lvalue-to-rvalue conversions and discarded-value conversions, |
| 969 | // so that we can avoid capturing certain constant variables. |
| 970 | // For e.g., |
| 971 | // void test() { |
| 972 | // const int x = 10; |
| 973 | // auto L = [&z = x](char a) { <-- don't capture by the current lambda |
| 974 | // return [y = x](int i) { <-- don't capture by enclosing lambda |
| 975 | // return y; |
| 976 | // } |
| 977 | // }; |
Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 978 | // } |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 979 | // If x was not const, the second use would require 'L' to capture, and |
| 980 | // that would be an error. |
| 981 | |
Richard Smith | 42b1057 | 2015-11-11 01:36:17 +0000 | [diff] [blame] | 982 | ParsedType InitCaptureType; |
Volodymyr Sapsai | b0f1aae | 2017-08-22 17:55:19 +0000 | [diff] [blame] | 983 | if (!Init.isInvalid()) |
| 984 | Init = Actions.CorrectDelayedTyposInExpr(Init.get()); |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 985 | if (Init.isUsable()) { |
| 986 | // Get the pointer and store it in an lvalue, so we can use it as an |
| 987 | // out argument. |
| 988 | Expr *InitExpr = Init.get(); |
| 989 | // This performs any lvalue-to-rvalue conversions if necessary, which |
| 990 | // can affect what gets captured in the containing decl-context. |
Richard Smith | 42b1057 | 2015-11-11 01:36:17 +0000 | [diff] [blame] | 991 | InitCaptureType = Actions.actOnLambdaInitCaptureInitialization( |
| 992 | Loc, Kind == LCK_ByRef, Id, InitKind, InitExpr); |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 993 | Init = InitExpr; |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 994 | } |
Alexander Shaposhnikov | 832f49b | 2018-07-16 07:23:47 +0000 | [diff] [blame] | 995 | |
| 996 | SourceLocation LocEnd = PrevTokLocation; |
| 997 | |
Richard Smith | 42b1057 | 2015-11-11 01:36:17 +0000 | [diff] [blame] | 998 | Intro.addCapture(Kind, Loc, Id, EllipsisLoc, InitKind, Init, |
Alexander Shaposhnikov | 832f49b | 2018-07-16 07:23:47 +0000 | [diff] [blame] | 999 | InitCaptureType, SourceRange(LocStart, LocEnd)); |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1002 | T.consumeClose(); |
| 1003 | Intro.Range.setEnd(T.getCloseLocation()); |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 1004 | return DiagResult(); |
| 1005 | } |
| 1006 | |
Douglas Gregor | d8c6178 | 2012-02-15 15:34:24 +0000 | [diff] [blame] | 1007 | /// TryParseLambdaIntroducer - Tentatively parse a lambda introducer. |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 1008 | /// |
| 1009 | /// Returns true if it hit something unexpected. |
| 1010 | bool Parser::TryParseLambdaIntroducer(LambdaIntroducer &Intro) { |
Jan Korous | 06aa2af | 2017-11-06 17:42:17 +0000 | [diff] [blame] | 1011 | { |
| 1012 | bool SkippedInits = false; |
| 1013 | TentativeParsingAction PA1(*this); |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 1014 | |
Jan Korous | 06aa2af | 2017-11-06 17:42:17 +0000 | [diff] [blame] | 1015 | if (ParseLambdaIntroducer(Intro, &SkippedInits)) { |
| 1016 | PA1.Revert(); |
| 1017 | return true; |
| 1018 | } |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 1019 | |
Jan Korous | 06aa2af | 2017-11-06 17:42:17 +0000 | [diff] [blame] | 1020 | if (!SkippedInits) { |
| 1021 | PA1.Commit(); |
| 1022 | return false; |
| 1023 | } |
| 1024 | |
| 1025 | PA1.Revert(); |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
Jan Korous | 06aa2af | 2017-11-06 17:42:17 +0000 | [diff] [blame] | 1028 | // Try to parse it again, but this time parse the init-captures too. |
| 1029 | Intro = LambdaIntroducer(); |
| 1030 | TentativeParsingAction PA2(*this); |
| 1031 | |
| 1032 | if (!ParseLambdaIntroducer(Intro)) { |
| 1033 | PA2.Commit(); |
Richard Smith | f44d2a8 | 2013-05-21 22:21:19 +0000 | [diff] [blame] | 1034 | return false; |
| 1035 | } |
| 1036 | |
Jan Korous | 06aa2af | 2017-11-06 17:42:17 +0000 | [diff] [blame] | 1037 | PA2.Revert(); |
| 1038 | return true; |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
Faisal Vali | a734ab9 | 2016-03-26 16:11:37 +0000 | [diff] [blame] | 1041 | static void |
| 1042 | tryConsumeMutableOrConstexprToken(Parser &P, SourceLocation &MutableLoc, |
| 1043 | SourceLocation &ConstexprLoc, |
| 1044 | SourceLocation &DeclEndLoc) { |
| 1045 | assert(MutableLoc.isInvalid()); |
| 1046 | assert(ConstexprLoc.isInvalid()); |
| 1047 | // Consume constexpr-opt mutable-opt in any sequence, and set the DeclEndLoc |
| 1048 | // to the final of those locations. Emit an error if we have multiple |
| 1049 | // copies of those keywords and recover. |
| 1050 | |
| 1051 | while (true) { |
| 1052 | switch (P.getCurToken().getKind()) { |
| 1053 | case tok::kw_mutable: { |
| 1054 | if (MutableLoc.isValid()) { |
| 1055 | P.Diag(P.getCurToken().getLocation(), |
| 1056 | diag::err_lambda_decl_specifier_repeated) |
| 1057 | << 0 << FixItHint::CreateRemoval(P.getCurToken().getLocation()); |
| 1058 | } |
| 1059 | MutableLoc = P.ConsumeToken(); |
| 1060 | DeclEndLoc = MutableLoc; |
| 1061 | break /*switch*/; |
| 1062 | } |
| 1063 | case tok::kw_constexpr: |
| 1064 | if (ConstexprLoc.isValid()) { |
| 1065 | P.Diag(P.getCurToken().getLocation(), |
| 1066 | diag::err_lambda_decl_specifier_repeated) |
| 1067 | << 1 << FixItHint::CreateRemoval(P.getCurToken().getLocation()); |
| 1068 | } |
| 1069 | ConstexprLoc = P.ConsumeToken(); |
| 1070 | DeclEndLoc = ConstexprLoc; |
| 1071 | break /*switch*/; |
| 1072 | default: |
| 1073 | return; |
| 1074 | } |
| 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | static void |
| 1079 | addConstexprToLambdaDeclSpecifier(Parser &P, SourceLocation ConstexprLoc, |
| 1080 | DeclSpec &DS) { |
| 1081 | if (ConstexprLoc.isValid()) { |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 1082 | P.Diag(ConstexprLoc, !P.getLangOpts().CPlusPlus17 |
Richard Smith | b115e5d | 2017-08-13 23:37:29 +0000 | [diff] [blame] | 1083 | ? diag::ext_constexpr_on_lambda_cxx17 |
Faisal Vali | a734ab9 | 2016-03-26 16:11:37 +0000 | [diff] [blame] | 1084 | : diag::warn_cxx14_compat_constexpr_on_lambda); |
| 1085 | const char *PrevSpec = nullptr; |
| 1086 | unsigned DiagID = 0; |
| 1087 | DS.SetConstexprSpec(ConstexprLoc, PrevSpec, DiagID); |
| 1088 | assert(PrevSpec == nullptr && DiagID == 0 && |
| 1089 | "Constexpr cannot have been set previously!"); |
| 1090 | } |
| 1091 | } |
| 1092 | |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 1093 | /// ParseLambdaExpressionAfterIntroducer - Parse the rest of a lambda |
| 1094 | /// expression. |
| 1095 | ExprResult Parser::ParseLambdaExpressionAfterIntroducer( |
| 1096 | LambdaIntroducer &Intro) { |
Eli Friedman | c7c9714 | 2012-01-04 02:40:39 +0000 | [diff] [blame] | 1097 | SourceLocation LambdaBeginLoc = Intro.Range.getBegin(); |
| 1098 | Diag(LambdaBeginLoc, diag::warn_cxx98_compat_lambda); |
| 1099 | |
| 1100 | PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), LambdaBeginLoc, |
| 1101 | "lambda expression parsing"); |
| 1102 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1103 | |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 1104 | |
Richard Smith | 21b3ab4 | 2013-05-09 21:36:41 +0000 | [diff] [blame] | 1105 | // FIXME: Call into Actions to add any init-capture declarations to the |
| 1106 | // scope while parsing the lambda-declarator and compound-statement. |
| 1107 | |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 1108 | // Parse lambda-declarator[opt]. |
| 1109 | DeclSpec DS(AttrFactory); |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 1110 | Declarator D(DS, DeclaratorContext::LambdaExprContext); |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 1111 | TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth); |
Justin Lebar | 0fad0ba | 2016-09-30 17:14:48 +0000 | [diff] [blame] | 1112 | Actions.PushLambdaScope(); |
| 1113 | |
| 1114 | ParsedAttributes Attr(AttrFactory); |
| 1115 | SourceLocation DeclLoc = Tok.getLocation(); |
Justin Lebar | 0fad0ba | 2016-09-30 17:14:48 +0000 | [diff] [blame] | 1116 | if (getLangOpts().CUDA) { |
| 1117 | // In CUDA code, GNU attributes are allowed to appear immediately after the |
| 1118 | // "[...]", even if there is no "(...)" before the lambda body. |
Justin Lebar | 0139a5d | 2016-09-30 19:55:48 +0000 | [diff] [blame] | 1119 | MaybeParseGNUAttributes(D); |
Justin Lebar | 0fad0ba | 2016-09-30 17:14:48 +0000 | [diff] [blame] | 1120 | } |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 1121 | |
Justin Lebar | e46ea72 | 2016-09-30 19:55:55 +0000 | [diff] [blame] | 1122 | // Helper to emit a warning if we see a CUDA host/device/global attribute |
| 1123 | // after '(...)'. nvcc doesn't accept this. |
| 1124 | auto WarnIfHasCUDATargetAttr = [&] { |
| 1125 | if (getLangOpts().CUDA) |
Erich Keane | e891aa9 | 2018-07-13 15:07:47 +0000 | [diff] [blame] | 1126 | for (const ParsedAttr &A : Attr) |
| 1127 | if (A.getKind() == ParsedAttr::AT_CUDADevice || |
| 1128 | A.getKind() == ParsedAttr::AT_CUDAHost || |
| 1129 | A.getKind() == ParsedAttr::AT_CUDAGlobal) |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1130 | Diag(A.getLoc(), diag::warn_cuda_attr_lambda_position) |
| 1131 | << A.getName()->getName(); |
Justin Lebar | e46ea72 | 2016-09-30 19:55:55 +0000 | [diff] [blame] | 1132 | }; |
| 1133 | |
Hamza Sood | 8205a81 | 2019-05-04 10:49:46 +0000 | [diff] [blame] | 1134 | // FIXME: Consider allowing this as an extension for GCC compatibiblity. |
| 1135 | const bool HasExplicitTemplateParams = Tok.is(tok::less); |
| 1136 | ParseScope TemplateParamScope(this, Scope::TemplateParamScope, |
| 1137 | /*EnteredScope=*/HasExplicitTemplateParams); |
| 1138 | if (HasExplicitTemplateParams) { |
| 1139 | Diag(Tok, getLangOpts().CPlusPlus2a |
| 1140 | ? diag::warn_cxx17_compat_lambda_template_parameter_list |
| 1141 | : diag::ext_lambda_template_parameter_list); |
| 1142 | |
| 1143 | SmallVector<NamedDecl*, 4> TemplateParams; |
| 1144 | SourceLocation LAngleLoc, RAngleLoc; |
| 1145 | if (ParseTemplateParameters(CurTemplateDepthTracker.getDepth(), |
| 1146 | TemplateParams, LAngleLoc, RAngleLoc)) { |
| 1147 | Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope()); |
| 1148 | return ExprError(); |
| 1149 | } |
| 1150 | |
| 1151 | if (TemplateParams.empty()) { |
| 1152 | Diag(RAngleLoc, |
| 1153 | diag::err_lambda_template_parameter_list_empty); |
| 1154 | } else { |
| 1155 | Actions.ActOnLambdaExplicitTemplateParameterList( |
| 1156 | LAngleLoc, TemplateParams, RAngleLoc); |
| 1157 | ++CurTemplateDepthTracker; |
| 1158 | } |
| 1159 | } |
| 1160 | |
David Majnemer | e01c466 | 2015-01-09 05:10:55 +0000 | [diff] [blame] | 1161 | TypeResult TrailingReturnType; |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 1162 | if (Tok.is(tok::l_paren)) { |
| 1163 | ParseScope PrototypeScope(this, |
| 1164 | Scope::FunctionPrototypeScope | |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1165 | Scope::FunctionDeclarationScope | |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 1166 | Scope::DeclScope); |
| 1167 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1168 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1169 | T.consumeOpen(); |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 1170 | SourceLocation LParenLoc = T.getOpenLocation(); |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 1171 | |
| 1172 | // Parse parameter-declaration-clause. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1173 | SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 1174 | SourceLocation EllipsisLoc; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1175 | |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 1176 | if (Tok.isNot(tok::r_paren)) { |
Hamza Sood | 8205a81 | 2019-05-04 10:49:46 +0000 | [diff] [blame] | 1177 | Actions.RecordParsingTemplateParameterDepth( |
| 1178 | CurTemplateDepthTracker.getOriginalDepth()); |
| 1179 | |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 1180 | ParseParameterDeclarationClause(D, Attr, ParamInfo, EllipsisLoc); |
Hamza Sood | 8205a81 | 2019-05-04 10:49:46 +0000 | [diff] [blame] | 1181 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1182 | // For a generic lambda, each 'auto' within the parameter declaration |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 1183 | // clause creates a template type parameter, so increment the depth. |
Hamza Sood | 8205a81 | 2019-05-04 10:49:46 +0000 | [diff] [blame] | 1184 | // If we've parsed any explicit template parameters, then the depth will |
| 1185 | // have already been incremented. So we make sure that at most a single |
| 1186 | // depth level is added. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1187 | if (Actions.getCurGenericLambda()) |
Hamza Sood | 8205a81 | 2019-05-04 10:49:46 +0000 | [diff] [blame] | 1188 | CurTemplateDepthTracker.setAddedDepth(1); |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 1189 | } |
Hamza Sood | 8205a81 | 2019-05-04 10:49:46 +0000 | [diff] [blame] | 1190 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1191 | T.consumeClose(); |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 1192 | SourceLocation RParenLoc = T.getCloseLocation(); |
Justin Lebar | 0139a5d | 2016-09-30 19:55:48 +0000 | [diff] [blame] | 1193 | SourceLocation DeclEndLoc = RParenLoc; |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 1194 | |
Aaron Ballman | e8d69b7 | 2014-03-12 00:01:07 +0000 | [diff] [blame] | 1195 | // GNU-style attributes must be parsed before the mutable specifier to be |
| 1196 | // compatible with GCC. |
| 1197 | MaybeParseGNUAttributes(Attr, &DeclEndLoc); |
| 1198 | |
David Majnemer | bda8632 | 2015-02-04 08:22:46 +0000 | [diff] [blame] | 1199 | // MSVC-style attributes must be parsed before the mutable specifier to be |
| 1200 | // compatible with MSVC. |
Aaron Ballman | 068aa51 | 2015-05-20 20:58:33 +0000 | [diff] [blame] | 1201 | MaybeParseMicrosoftDeclSpecs(Attr, &DeclEndLoc); |
David Majnemer | bda8632 | 2015-02-04 08:22:46 +0000 | [diff] [blame] | 1202 | |
Faisal Vali | a734ab9 | 2016-03-26 16:11:37 +0000 | [diff] [blame] | 1203 | // Parse mutable-opt and/or constexpr-opt, and update the DeclEndLoc. |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 1204 | SourceLocation MutableLoc; |
Faisal Vali | a734ab9 | 2016-03-26 16:11:37 +0000 | [diff] [blame] | 1205 | SourceLocation ConstexprLoc; |
| 1206 | tryConsumeMutableOrConstexprToken(*this, MutableLoc, ConstexprLoc, |
| 1207 | DeclEndLoc); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1208 | |
Faisal Vali | a734ab9 | 2016-03-26 16:11:37 +0000 | [diff] [blame] | 1209 | addConstexprToLambdaDeclSpecifier(*this, ConstexprLoc, DS); |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 1210 | |
| 1211 | // Parse exception-specification[opt]. |
| 1212 | ExceptionSpecificationType ESpecType = EST_None; |
| 1213 | SourceRange ESpecRange; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1214 | SmallVector<ParsedType, 2> DynamicExceptions; |
| 1215 | SmallVector<SourceRange, 2> DynamicExceptionRanges; |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 1216 | ExprResult NoexceptExpr; |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 1217 | CachedTokens *ExceptionSpecTokens; |
| 1218 | ESpecType = tryParseExceptionSpecification(/*Delayed=*/false, |
| 1219 | ESpecRange, |
Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 1220 | DynamicExceptions, |
| 1221 | DynamicExceptionRanges, |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 1222 | NoexceptExpr, |
| 1223 | ExceptionSpecTokens); |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 1224 | |
| 1225 | if (ESpecType != EST_None) |
| 1226 | DeclEndLoc = ESpecRange.getEnd(); |
| 1227 | |
| 1228 | // Parse attribute-specifier[opt]. |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1229 | MaybeParseCXX11Attributes(Attr, &DeclEndLoc); |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 1230 | |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 1231 | SourceLocation FunLocalRangeEnd = DeclEndLoc; |
| 1232 | |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 1233 | // Parse trailing-return-type[opt]. |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 1234 | if (Tok.is(tok::arrow)) { |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 1235 | FunLocalRangeEnd = Tok.getLocation(); |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 1236 | SourceRange Range; |
Richard Smith | e303e35 | 2018-02-02 22:24:54 +0000 | [diff] [blame] | 1237 | TrailingReturnType = |
| 1238 | ParseTrailingReturnType(Range, /*MayBeFollowedByDirectInit*/ false); |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 1239 | if (Range.getEnd().isValid()) |
| 1240 | DeclEndLoc = Range.getEnd(); |
| 1241 | } |
| 1242 | |
| 1243 | PrototypeScope.Exit(); |
| 1244 | |
Justin Lebar | e46ea72 | 2016-09-30 19:55:55 +0000 | [diff] [blame] | 1245 | WarnIfHasCUDATargetAttr(); |
| 1246 | |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 1247 | SourceLocation NoLoc; |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1248 | D.AddTypeInfo(DeclaratorChunk::getFunction( |
| 1249 | /*hasProto=*/true, |
| 1250 | /*isAmbiguous=*/false, LParenLoc, ParamInfo.data(), |
| 1251 | ParamInfo.size(), EllipsisLoc, RParenLoc, |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1252 | /*RefQualifierIsLValueRef=*/true, |
Anastasia Stulova | a9bc4bd | 2019-01-09 11:25:09 +0000 | [diff] [blame] | 1253 | /*RefQualifierLoc=*/NoLoc, MutableLoc, ESpecType, |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1254 | ESpecRange, DynamicExceptions.data(), |
| 1255 | DynamicExceptionRanges.data(), DynamicExceptions.size(), |
| 1256 | NoexceptExpr.isUsable() ? NoexceptExpr.get() : nullptr, |
| 1257 | /*ExceptionSpecTokens*/ nullptr, |
| 1258 | /*DeclsInPrototype=*/None, LParenLoc, FunLocalRangeEnd, D, |
| 1259 | TrailingReturnType), |
| 1260 | std::move(Attr), DeclEndLoc); |
Faisal Vali | a734ab9 | 2016-03-26 16:11:37 +0000 | [diff] [blame] | 1261 | } else if (Tok.isOneOf(tok::kw_mutable, tok::arrow, tok::kw___attribute, |
| 1262 | tok::kw_constexpr) || |
Aaron Ballman | b5c59f5 | 2014-03-11 13:03:15 +0000 | [diff] [blame] | 1263 | (Tok.is(tok::l_square) && NextToken().is(tok::l_square))) { |
| 1264 | // It's common to forget that one needs '()' before 'mutable', an attribute |
| 1265 | // specifier, or the result type. Deal with this. |
| 1266 | unsigned TokKind = 0; |
| 1267 | switch (Tok.getKind()) { |
| 1268 | case tok::kw_mutable: TokKind = 0; break; |
| 1269 | case tok::arrow: TokKind = 1; break; |
Aaron Ballman | e8d69b7 | 2014-03-12 00:01:07 +0000 | [diff] [blame] | 1270 | case tok::kw___attribute: |
Aaron Ballman | b5c59f5 | 2014-03-11 13:03:15 +0000 | [diff] [blame] | 1271 | case tok::l_square: TokKind = 2; break; |
Faisal Vali | a734ab9 | 2016-03-26 16:11:37 +0000 | [diff] [blame] | 1272 | case tok::kw_constexpr: TokKind = 3; break; |
Aaron Ballman | b5c59f5 | 2014-03-11 13:03:15 +0000 | [diff] [blame] | 1273 | default: llvm_unreachable("Unknown token kind"); |
| 1274 | } |
| 1275 | |
Douglas Gregor | 6746c5d | 2012-02-16 21:53:36 +0000 | [diff] [blame] | 1276 | Diag(Tok, diag::err_lambda_missing_parens) |
Aaron Ballman | b5c59f5 | 2014-03-11 13:03:15 +0000 | [diff] [blame] | 1277 | << TokKind |
Douglas Gregor | 6746c5d | 2012-02-16 21:53:36 +0000 | [diff] [blame] | 1278 | << FixItHint::CreateInsertion(Tok.getLocation(), "() "); |
Justin Lebar | 0139a5d | 2016-09-30 19:55:48 +0000 | [diff] [blame] | 1279 | SourceLocation DeclEndLoc = DeclLoc; |
Aaron Ballman | e8d69b7 | 2014-03-12 00:01:07 +0000 | [diff] [blame] | 1280 | |
| 1281 | // GNU-style attributes must be parsed before the mutable specifier to be |
| 1282 | // compatible with GCC. |
Aaron Ballman | e8d69b7 | 2014-03-12 00:01:07 +0000 | [diff] [blame] | 1283 | MaybeParseGNUAttributes(Attr, &DeclEndLoc); |
| 1284 | |
Douglas Gregor | 6746c5d | 2012-02-16 21:53:36 +0000 | [diff] [blame] | 1285 | // Parse 'mutable', if it's there. |
| 1286 | SourceLocation MutableLoc; |
| 1287 | if (Tok.is(tok::kw_mutable)) { |
| 1288 | MutableLoc = ConsumeToken(); |
| 1289 | DeclEndLoc = MutableLoc; |
| 1290 | } |
Aaron Ballman | b5c59f5 | 2014-03-11 13:03:15 +0000 | [diff] [blame] | 1291 | |
| 1292 | // Parse attribute-specifier[opt]. |
Aaron Ballman | b5c59f5 | 2014-03-11 13:03:15 +0000 | [diff] [blame] | 1293 | MaybeParseCXX11Attributes(Attr, &DeclEndLoc); |
| 1294 | |
Douglas Gregor | 6746c5d | 2012-02-16 21:53:36 +0000 | [diff] [blame] | 1295 | // Parse the return type, if there is one. |
Douglas Gregor | 6746c5d | 2012-02-16 21:53:36 +0000 | [diff] [blame] | 1296 | if (Tok.is(tok::arrow)) { |
| 1297 | SourceRange Range; |
Richard Smith | e303e35 | 2018-02-02 22:24:54 +0000 | [diff] [blame] | 1298 | TrailingReturnType = |
| 1299 | ParseTrailingReturnType(Range, /*MayBeFollowedByDirectInit*/ false); |
Douglas Gregor | 6746c5d | 2012-02-16 21:53:36 +0000 | [diff] [blame] | 1300 | if (Range.getEnd().isValid()) |
David Majnemer | e01c466 | 2015-01-09 05:10:55 +0000 | [diff] [blame] | 1301 | DeclEndLoc = Range.getEnd(); |
Douglas Gregor | 6746c5d | 2012-02-16 21:53:36 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
Justin Lebar | e46ea72 | 2016-09-30 19:55:55 +0000 | [diff] [blame] | 1304 | WarnIfHasCUDATargetAttr(); |
| 1305 | |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 1306 | SourceLocation NoLoc; |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1307 | D.AddTypeInfo(DeclaratorChunk::getFunction( |
| 1308 | /*hasProto=*/true, |
| 1309 | /*isAmbiguous=*/false, |
| 1310 | /*LParenLoc=*/NoLoc, |
| 1311 | /*Params=*/nullptr, |
| 1312 | /*NumParams=*/0, |
| 1313 | /*EllipsisLoc=*/NoLoc, |
| 1314 | /*RParenLoc=*/NoLoc, |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1315 | /*RefQualifierIsLValueRef=*/true, |
Anastasia Stulova | a9bc4bd | 2019-01-09 11:25:09 +0000 | [diff] [blame] | 1316 | /*RefQualifierLoc=*/NoLoc, MutableLoc, EST_None, |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1317 | /*ESpecRange=*/SourceRange(), |
| 1318 | /*Exceptions=*/nullptr, |
| 1319 | /*ExceptionRanges=*/nullptr, |
| 1320 | /*NumExceptions=*/0, |
| 1321 | /*NoexceptExpr=*/nullptr, |
| 1322 | /*ExceptionSpecTokens=*/nullptr, |
| 1323 | /*DeclsInPrototype=*/None, DeclLoc, DeclEndLoc, D, |
| 1324 | TrailingReturnType), |
| 1325 | std::move(Attr), DeclEndLoc); |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
Eli Friedman | 4817cf7 | 2012-01-06 03:05:34 +0000 | [diff] [blame] | 1328 | // FIXME: Rename BlockScope -> ClosureScope if we decide to continue using |
| 1329 | // it. |
Momchil Velikov | 57c681f | 2017-08-10 15:43:06 +0000 | [diff] [blame] | 1330 | unsigned ScopeFlags = Scope::BlockScope | Scope::FnScope | Scope::DeclScope | |
| 1331 | Scope::CompoundStmtScope; |
Douglas Gregor | b838997 | 2012-02-21 22:51:27 +0000 | [diff] [blame] | 1332 | ParseScope BodyScope(this, ScopeFlags); |
Eli Friedman | 4817cf7 | 2012-01-06 03:05:34 +0000 | [diff] [blame] | 1333 | |
Eli Friedman | 71c8055 | 2012-01-05 03:35:19 +0000 | [diff] [blame] | 1334 | Actions.ActOnStartOfLambdaDefinition(Intro, D, getCurScope()); |
| 1335 | |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 1336 | // Parse compound-statement. |
Eli Friedman | c7c9714 | 2012-01-04 02:40:39 +0000 | [diff] [blame] | 1337 | if (!Tok.is(tok::l_brace)) { |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 1338 | Diag(Tok, diag::err_expected_lambda_body); |
Eli Friedman | c7c9714 | 2012-01-04 02:40:39 +0000 | [diff] [blame] | 1339 | Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope()); |
| 1340 | return ExprError(); |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 1341 | } |
| 1342 | |
Eli Friedman | c7c9714 | 2012-01-04 02:40:39 +0000 | [diff] [blame] | 1343 | StmtResult Stmt(ParseCompoundStatementBody()); |
| 1344 | BodyScope.Exit(); |
Hamza Sood | 8205a81 | 2019-05-04 10:49:46 +0000 | [diff] [blame] | 1345 | TemplateParamScope.Exit(); |
Eli Friedman | c7c9714 | 2012-01-04 02:40:39 +0000 | [diff] [blame] | 1346 | |
David Majnemer | e01c466 | 2015-01-09 05:10:55 +0000 | [diff] [blame] | 1347 | if (!Stmt.isInvalid() && !TrailingReturnType.isInvalid()) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1348 | return Actions.ActOnLambdaExpr(LambdaBeginLoc, Stmt.get(), getCurScope()); |
David Majnemer | e01c466 | 2015-01-09 05:10:55 +0000 | [diff] [blame] | 1349 | |
Eli Friedman | 898caf8 | 2012-01-04 02:46:53 +0000 | [diff] [blame] | 1350 | Actions.ActOnLambdaError(LambdaBeginLoc, getCurScope()); |
| 1351 | return ExprError(); |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 1352 | } |
| 1353 | |
Chris Lattner | 2937565 | 2006-12-04 18:06:35 +0000 | [diff] [blame] | 1354 | /// ParseCXXCasts - This handles the various ways to cast expressions to another |
| 1355 | /// type. |
| 1356 | /// |
| 1357 | /// postfix-expression: [C++ 5.2p1] |
| 1358 | /// 'dynamic_cast' '<' type-name '>' '(' expression ')' |
| 1359 | /// 'static_cast' '<' type-name '>' '(' expression ')' |
| 1360 | /// 'reinterpret_cast' '<' type-name '>' '(' expression ')' |
| 1361 | /// 'const_cast' '<' type-name '>' '(' expression ')' |
| 1362 | /// |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1363 | ExprResult Parser::ParseCXXCasts() { |
Chris Lattner | 2937565 | 2006-12-04 18:06:35 +0000 | [diff] [blame] | 1364 | tok::TokenKind Kind = Tok.getKind(); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1365 | const char *CastName = nullptr; // For error messages |
Chris Lattner | 2937565 | 2006-12-04 18:06:35 +0000 | [diff] [blame] | 1366 | |
| 1367 | switch (Kind) { |
David Blaikie | aa347f9 | 2011-09-23 20:26:49 +0000 | [diff] [blame] | 1368 | default: llvm_unreachable("Unknown C++ cast!"); |
Chris Lattner | 2937565 | 2006-12-04 18:06:35 +0000 | [diff] [blame] | 1369 | case tok::kw_const_cast: CastName = "const_cast"; break; |
| 1370 | case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break; |
| 1371 | case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break; |
| 1372 | case tok::kw_static_cast: CastName = "static_cast"; break; |
| 1373 | } |
| 1374 | |
| 1375 | SourceLocation OpLoc = ConsumeToken(); |
| 1376 | SourceLocation LAngleBracketLoc = Tok.getLocation(); |
| 1377 | |
Richard Smith | 5585849 | 2011-04-14 21:45:45 +0000 | [diff] [blame] | 1378 | // Check for "<::" which is parsed as "[:". If found, fix token stream, |
| 1379 | // diagnose error, suggest fix, and recover parsing. |
Richard Smith | 62e6630 | 2012-08-20 17:37:52 +0000 | [diff] [blame] | 1380 | if (Tok.is(tok::l_square) && Tok.getLength() == 2) { |
| 1381 | Token Next = NextToken(); |
| 1382 | if (Next.is(tok::colon) && areTokensAdjacent(Tok, Next)) |
| 1383 | FixDigraph(*this, PP, Tok, Next, Kind, /*AtDigraph*/true); |
| 1384 | } |
Richard Smith | 5585849 | 2011-04-14 21:45:45 +0000 | [diff] [blame] | 1385 | |
Chris Lattner | 2937565 | 2006-12-04 18:06:35 +0000 | [diff] [blame] | 1386 | if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName)) |
Sebastian Redl | d65cea8 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 1387 | return ExprError(); |
Chris Lattner | 2937565 | 2006-12-04 18:06:35 +0000 | [diff] [blame] | 1388 | |
Argyrios Kyrtzidis | 7451d1c | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 1389 | // Parse the common declaration-specifiers piece. |
| 1390 | DeclSpec DS(AttrFactory); |
| 1391 | ParseSpecifierQualifierList(DS); |
| 1392 | |
| 1393 | // Parse the abstract-declarator, if present. |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 1394 | Declarator DeclaratorInfo(DS, DeclaratorContext::TypeNameContext); |
Argyrios Kyrtzidis | 7451d1c | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 1395 | ParseDeclarator(DeclaratorInfo); |
| 1396 | |
Chris Lattner | 2937565 | 2006-12-04 18:06:35 +0000 | [diff] [blame] | 1397 | SourceLocation RAngleBracketLoc = Tok.getLocation(); |
| 1398 | |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 1399 | if (ExpectAndConsume(tok::greater)) |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1400 | return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << tok::less); |
Chris Lattner | 2937565 | 2006-12-04 18:06:35 +0000 | [diff] [blame] | 1401 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1402 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Chris Lattner | 2937565 | 2006-12-04 18:06:35 +0000 | [diff] [blame] | 1403 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1404 | if (T.expectAndConsume(diag::err_expected_lparen_after, CastName)) |
Argyrios Kyrtzidis | 387a334 | 2009-05-22 10:23:16 +0000 | [diff] [blame] | 1405 | return ExprError(); |
Chris Lattner | 2937565 | 2006-12-04 18:06:35 +0000 | [diff] [blame] | 1406 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1407 | ExprResult Result = ParseExpression(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1408 | |
Argyrios Kyrtzidis | 387a334 | 2009-05-22 10:23:16 +0000 | [diff] [blame] | 1409 | // Match the ')'. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1410 | T.consumeClose(); |
Chris Lattner | 2937565 | 2006-12-04 18:06:35 +0000 | [diff] [blame] | 1411 | |
Argyrios Kyrtzidis | 7451d1c | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 1412 | if (!Result.isInvalid() && !DeclaratorInfo.isInvalidType()) |
Douglas Gregor | e200adc | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 1413 | Result = Actions.ActOnCXXNamedCast(OpLoc, Kind, |
Argyrios Kyrtzidis | 7451d1c | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 1414 | LAngleBracketLoc, DeclaratorInfo, |
Douglas Gregor | 220cac5 | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 1415 | RAngleBracketLoc, |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1416 | T.getOpenLocation(), Result.get(), |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1417 | T.getCloseLocation()); |
Chris Lattner | 2937565 | 2006-12-04 18:06:35 +0000 | [diff] [blame] | 1418 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1419 | return Result; |
Chris Lattner | 2937565 | 2006-12-04 18:06:35 +0000 | [diff] [blame] | 1420 | } |
Bill Wendling | 4073ed5 | 2007-02-13 01:51:42 +0000 | [diff] [blame] | 1421 | |
Sebastian Redl | c470476 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 1422 | /// ParseCXXTypeid - This handles the C++ typeid expression. |
| 1423 | /// |
| 1424 | /// postfix-expression: [C++ 5.2p1] |
| 1425 | /// 'typeid' '(' expression ')' |
| 1426 | /// 'typeid' '(' type-id ')' |
| 1427 | /// |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1428 | ExprResult Parser::ParseCXXTypeid() { |
Sebastian Redl | c470476 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 1429 | assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!"); |
| 1430 | |
| 1431 | SourceLocation OpLoc = ConsumeToken(); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1432 | SourceLocation LParenLoc, RParenLoc; |
| 1433 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Sebastian Redl | c470476 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 1434 | |
| 1435 | // typeid expressions are always parenthesized. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1436 | if (T.expectAndConsume(diag::err_expected_lparen_after, "typeid")) |
Sebastian Redl | d65cea8 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 1437 | return ExprError(); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1438 | LParenLoc = T.getOpenLocation(); |
Sebastian Redl | c470476 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 1439 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1440 | ExprResult Result; |
Sebastian Redl | c470476 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 1441 | |
Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 1442 | // C++0x [expr.typeid]p3: |
| 1443 | // When typeid is applied to an expression other than an lvalue of a |
| 1444 | // polymorphic class type [...] The expression is an unevaluated |
| 1445 | // operand (Clause 5). |
| 1446 | // |
| 1447 | // Note that we can't tell whether the expression is an lvalue of a |
| 1448 | // polymorphic class type until after we've parsed the expression; we |
| 1449 | // speculatively assume the subexpression is unevaluated, and fix it up |
| 1450 | // later. |
| 1451 | // |
| 1452 | // We enter the unevaluated context before trying to determine whether we |
| 1453 | // have a type-id, because the tentative parse logic will try to resolve |
| 1454 | // names, and must treat them as unevaluated. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 1455 | EnterExpressionEvaluationContext Unevaluated( |
| 1456 | Actions, Sema::ExpressionEvaluationContext::Unevaluated, |
| 1457 | Sema::ReuseLambdaContextDecl); |
Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 1458 | |
Sebastian Redl | c470476 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 1459 | if (isTypeIdInParens()) { |
Douglas Gregor | 220cac5 | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 1460 | TypeResult Ty = ParseTypeName(); |
Sebastian Redl | c470476 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 1461 | |
| 1462 | // Match the ')'. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1463 | T.consumeClose(); |
| 1464 | RParenLoc = T.getCloseLocation(); |
Douglas Gregor | 4c7c109 | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 1465 | if (Ty.isInvalid() || RParenLoc.isInvalid()) |
Sebastian Redl | d65cea8 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 1466 | return ExprError(); |
Sebastian Redl | c470476 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 1467 | |
| 1468 | Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1469 | Ty.get().getAsOpaquePtr(), RParenLoc); |
Sebastian Redl | c470476 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 1470 | } else { |
| 1471 | Result = ParseExpression(); |
| 1472 | |
| 1473 | // Match the ')'. |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1474 | if (Result.isInvalid()) |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1475 | SkipUntil(tok::r_paren, StopAtSemi); |
Sebastian Redl | c470476 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 1476 | else { |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1477 | T.consumeClose(); |
| 1478 | RParenLoc = T.getCloseLocation(); |
Douglas Gregor | 4c7c109 | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 1479 | if (RParenLoc.isInvalid()) |
| 1480 | return ExprError(); |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 1481 | |
Sebastian Redl | c470476 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 1482 | Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1483 | Result.get(), RParenLoc); |
Sebastian Redl | c470476 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 1484 | } |
| 1485 | } |
| 1486 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1487 | return Result; |
Sebastian Redl | c470476 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 1490 | /// ParseCXXUuidof - This handles the Microsoft C++ __uuidof expression. |
| 1491 | /// |
| 1492 | /// '__uuidof' '(' expression ')' |
| 1493 | /// '__uuidof' '(' type-id ')' |
| 1494 | /// |
| 1495 | ExprResult Parser::ParseCXXUuidof() { |
| 1496 | assert(Tok.is(tok::kw___uuidof) && "Not '__uuidof'!"); |
| 1497 | |
| 1498 | SourceLocation OpLoc = ConsumeToken(); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1499 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 1500 | |
| 1501 | // __uuidof expressions are always parenthesized. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1502 | if (T.expectAndConsume(diag::err_expected_lparen_after, "__uuidof")) |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 1503 | return ExprError(); |
| 1504 | |
| 1505 | ExprResult Result; |
| 1506 | |
| 1507 | if (isTypeIdInParens()) { |
| 1508 | TypeResult Ty = ParseTypeName(); |
| 1509 | |
| 1510 | // Match the ')'. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1511 | T.consumeClose(); |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 1512 | |
| 1513 | if (Ty.isInvalid()) |
| 1514 | return ExprError(); |
| 1515 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1516 | Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(), /*isType=*/true, |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1517 | Ty.get().getAsOpaquePtr(), |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1518 | T.getCloseLocation()); |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 1519 | } else { |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 1520 | EnterExpressionEvaluationContext Unevaluated( |
| 1521 | Actions, Sema::ExpressionEvaluationContext::Unevaluated); |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 1522 | Result = ParseExpression(); |
| 1523 | |
| 1524 | // Match the ')'. |
| 1525 | if (Result.isInvalid()) |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1526 | SkipUntil(tok::r_paren, StopAtSemi); |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 1527 | else { |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1528 | T.consumeClose(); |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 1529 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1530 | Result = Actions.ActOnCXXUuidof(OpLoc, T.getOpenLocation(), |
| 1531 | /*isType=*/false, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1532 | Result.get(), T.getCloseLocation()); |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 1533 | } |
| 1534 | } |
| 1535 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1536 | return Result; |
Francois Pichet | 9f4f207 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1539 | /// Parse a C++ pseudo-destructor expression after the base, |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 1540 | /// . or -> operator, and nested-name-specifier have already been |
| 1541 | /// parsed. |
| 1542 | /// |
| 1543 | /// postfix-expression: [C++ 5.2] |
| 1544 | /// postfix-expression . pseudo-destructor-name |
| 1545 | /// postfix-expression -> pseudo-destructor-name |
| 1546 | /// |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1547 | /// pseudo-destructor-name: |
| 1548 | /// ::[opt] nested-name-specifier[opt] type-name :: ~type-name |
| 1549 | /// ::[opt] nested-name-specifier template simple-template-id :: |
| 1550 | /// ~type-name |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 1551 | /// ::[opt] nested-name-specifier[opt] ~type-name |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1552 | /// |
| 1553 | ExprResult |
Craig Topper | a2c5153 | 2014-10-30 05:30:05 +0000 | [diff] [blame] | 1554 | Parser::ParseCXXPseudoDestructor(Expr *Base, SourceLocation OpLoc, |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 1555 | tok::TokenKind OpKind, |
| 1556 | CXXScopeSpec &SS, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1557 | ParsedType ObjectType) { |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 1558 | // We're parsing either a pseudo-destructor-name or a dependent |
| 1559 | // member access that has the same form as a |
| 1560 | // pseudo-destructor-name. We parse both in the same way and let |
| 1561 | // the action model sort them out. |
| 1562 | // |
| 1563 | // Note that the ::[opt] nested-name-specifier[opt] has already |
| 1564 | // been parsed, and if there was a simple-template-id, it has |
| 1565 | // been coalesced into a template-id annotation token. |
| 1566 | UnqualifiedId FirstTypeName; |
| 1567 | SourceLocation CCLoc; |
| 1568 | if (Tok.is(tok::identifier)) { |
| 1569 | FirstTypeName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 1570 | ConsumeToken(); |
| 1571 | assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail"); |
| 1572 | CCLoc = ConsumeToken(); |
| 1573 | } else if (Tok.is(tok::annot_template_id)) { |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1574 | // FIXME: retrieve TemplateKWLoc from template-id annotation and |
| 1575 | // store it in the pseudo-dtor node (to be used when instantiating it). |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 1576 | FirstTypeName.setTemplateId( |
| 1577 | (TemplateIdAnnotation *)Tok.getAnnotationValue()); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 1578 | ConsumeAnnotationToken(); |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 1579 | assert(Tok.is(tok::coloncolon) &&"ParseOptionalCXXScopeSpecifier fail"); |
| 1580 | CCLoc = ConsumeToken(); |
| 1581 | } else { |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1582 | FirstTypeName.setIdentifier(nullptr, SourceLocation()); |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 1583 | } |
| 1584 | |
| 1585 | // Parse the tilde. |
| 1586 | assert(Tok.is(tok::tilde) && "ParseOptionalCXXScopeSpecifier fail"); |
| 1587 | SourceLocation TildeLoc = ConsumeToken(); |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 1588 | |
| 1589 | if (Tok.is(tok::kw_decltype) && !FirstTypeName.isValid() && SS.isEmpty()) { |
| 1590 | DeclSpec DS(AttrFactory); |
Benjamin Kramer | 198e083 | 2011-12-18 12:18:02 +0000 | [diff] [blame] | 1591 | ParseDecltypeSpecifier(DS); |
Faisal Vali | 090da2d | 2018-01-01 18:23:28 +0000 | [diff] [blame] | 1592 | if (DS.getTypeSpecType() == TST_error) |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 1593 | return ExprError(); |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 1594 | return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base, OpLoc, OpKind, |
| 1595 | TildeLoc, DS); |
David Blaikie | 1d57878 | 2011-12-16 16:03:09 +0000 | [diff] [blame] | 1596 | } |
| 1597 | |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 1598 | if (!Tok.is(tok::identifier)) { |
| 1599 | Diag(Tok, diag::err_destructor_tilde_identifier); |
| 1600 | return ExprError(); |
| 1601 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1602 | |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 1603 | // Parse the second type. |
| 1604 | UnqualifiedId SecondTypeName; |
| 1605 | IdentifierInfo *Name = Tok.getIdentifierInfo(); |
| 1606 | SourceLocation NameLoc = ConsumeToken(); |
| 1607 | SecondTypeName.setIdentifier(Name, NameLoc); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1608 | |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 1609 | // If there is a '<', the second type name is a template-id. Parse |
| 1610 | // it as such. |
| 1611 | if (Tok.is(tok::less) && |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1612 | ParseUnqualifiedIdTemplateId(SS, SourceLocation(), |
| 1613 | Name, NameLoc, |
| 1614 | false, ObjectType, SecondTypeName, |
| 1615 | /*AssumeTemplateName=*/true)) |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 1616 | return ExprError(); |
| 1617 | |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 1618 | return Actions.ActOnPseudoDestructorExpr(getCurScope(), Base, OpLoc, OpKind, |
| 1619 | SS, FirstTypeName, CCLoc, TildeLoc, |
| 1620 | SecondTypeName); |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 1621 | } |
| 1622 | |
Bill Wendling | 4073ed5 | 2007-02-13 01:51:42 +0000 | [diff] [blame] | 1623 | /// ParseCXXBoolLiteral - This handles the C++ Boolean literals. |
| 1624 | /// |
| 1625 | /// boolean-literal: [C++ 2.13.5] |
| 1626 | /// 'true' |
| 1627 | /// 'false' |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1628 | ExprResult Parser::ParseCXXBoolLiteral() { |
Bill Wendling | 4073ed5 | 2007-02-13 01:51:42 +0000 | [diff] [blame] | 1629 | tok::TokenKind Kind = Tok.getKind(); |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1630 | return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind); |
Bill Wendling | 4073ed5 | 2007-02-13 01:51:42 +0000 | [diff] [blame] | 1631 | } |
Chris Lattner | b7e656b | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 1632 | |
| 1633 | /// ParseThrowExpression - This handles the C++ throw expression. |
| 1634 | /// |
| 1635 | /// throw-expression: [C++ 15] |
| 1636 | /// 'throw' assignment-expression[opt] |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1637 | ExprResult Parser::ParseThrowExpression() { |
Chris Lattner | b7e656b | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 1638 | assert(Tok.is(tok::kw_throw) && "Not throw!"); |
Chris Lattner | b7e656b | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 1639 | SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token. |
Sebastian Redl | d65cea8 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 1640 | |
Chris Lattner | 65dd843 | 2008-04-06 06:02:23 +0000 | [diff] [blame] | 1641 | // If the current token isn't the start of an assignment-expression, |
| 1642 | // then the expression is not present. This handles things like: |
| 1643 | // "C ? throw : (void)42", which is crazy but legal. |
| 1644 | switch (Tok.getKind()) { // FIXME: move this predicate somewhere common. |
| 1645 | case tok::semi: |
| 1646 | case tok::r_paren: |
| 1647 | case tok::r_square: |
| 1648 | case tok::r_brace: |
| 1649 | case tok::colon: |
| 1650 | case tok::comma: |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 1651 | return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, nullptr); |
Chris Lattner | b7e656b | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 1652 | |
Chris Lattner | 65dd843 | 2008-04-06 06:02:23 +0000 | [diff] [blame] | 1653 | default: |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1654 | ExprResult Expr(ParseAssignmentExpression()); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1655 | if (Expr.isInvalid()) return Expr; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1656 | return Actions.ActOnCXXThrow(getCurScope(), ThrowLoc, Expr.get()); |
Chris Lattner | 65dd843 | 2008-04-06 06:02:23 +0000 | [diff] [blame] | 1657 | } |
Chris Lattner | b7e656b | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 1658 | } |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1659 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1660 | /// Parse the C++ Coroutines co_yield expression. |
Richard Smith | 0e304ea | 2015-10-22 04:46:14 +0000 | [diff] [blame] | 1661 | /// |
| 1662 | /// co_yield-expression: |
| 1663 | /// 'co_yield' assignment-expression[opt] |
| 1664 | ExprResult Parser::ParseCoyieldExpression() { |
| 1665 | assert(Tok.is(tok::kw_co_yield) && "Not co_yield!"); |
| 1666 | |
| 1667 | SourceLocation Loc = ConsumeToken(); |
Richard Smith | ae3d147 | 2015-11-20 22:47:10 +0000 | [diff] [blame] | 1668 | ExprResult Expr = Tok.is(tok::l_brace) ? ParseBraceInitializer() |
| 1669 | : ParseAssignmentExpression(); |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 1670 | if (!Expr.isInvalid()) |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 1671 | Expr = Actions.ActOnCoyieldExpr(getCurScope(), Loc, Expr.get()); |
Richard Smith | 0e304ea | 2015-10-22 04:46:14 +0000 | [diff] [blame] | 1672 | return Expr; |
| 1673 | } |
| 1674 | |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1675 | /// ParseCXXThis - This handles the C++ 'this' pointer. |
| 1676 | /// |
| 1677 | /// C++ 9.3.2: In the body of a non-static member function, the keyword this is |
| 1678 | /// a non-lvalue expression whose value is the address of the object for which |
| 1679 | /// the function is called. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1680 | ExprResult Parser::ParseCXXThis() { |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1681 | assert(Tok.is(tok::kw_this) && "Not 'this'!"); |
| 1682 | SourceLocation ThisLoc = ConsumeToken(); |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1683 | return Actions.ActOnCXXThis(ThisLoc); |
Argyrios Kyrtzidis | 7bbb20e | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 1684 | } |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1685 | |
| 1686 | /// ParseCXXTypeConstructExpression - Parse construction of a specified type. |
| 1687 | /// Can be interpreted either as function-style casting ("int(x)") |
| 1688 | /// or class type construction ("ClassType(x,y,z)") |
| 1689 | /// or creation of a value-initialized type ("int()"). |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1690 | /// See [C++ 5.2.3]. |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1691 | /// |
| 1692 | /// postfix-expression: [C++ 5.2p1] |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1693 | /// simple-type-specifier '(' expression-list[opt] ')' |
| 1694 | /// [C++0x] simple-type-specifier braced-init-list |
| 1695 | /// typename-specifier '(' expression-list[opt] ')' |
| 1696 | /// [C++0x] typename-specifier braced-init-list |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1697 | /// |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 1698 | /// In C++1z onwards, the type specifier can also be a template-name. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1699 | ExprResult |
Sebastian Redl | d65cea8 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 1700 | Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) { |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 1701 | Declarator DeclaratorInfo(DS, DeclaratorContext::FunctionalCastContext); |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1702 | ParsedType TypeRep = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get(); |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1703 | |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1704 | assert((Tok.is(tok::l_paren) || |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1705 | (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace))) |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1706 | && "Expected '(' or '{'!"); |
Douglas Gregor | 94a3247 | 2011-01-11 00:33:19 +0000 | [diff] [blame] | 1707 | |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1708 | if (Tok.is(tok::l_brace)) { |
Sebastian Redl | d74dd49 | 2012-02-12 18:41:05 +0000 | [diff] [blame] | 1709 | ExprResult Init = ParseBraceInitializer(); |
| 1710 | if (Init.isInvalid()) |
| 1711 | return Init; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1712 | Expr *InitList = Init.get(); |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 1713 | return Actions.ActOnCXXTypeConstructExpr( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1714 | TypeRep, InitList->getBeginLoc(), MultiExprArg(&InitList, 1), |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1715 | InitList->getEndLoc(), /*ListInitialization=*/true); |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1716 | } else { |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1717 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1718 | T.consumeOpen(); |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1719 | |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 1720 | PreferredType.enterTypeCast(Tok.getLocation(), TypeRep.get()); |
| 1721 | |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 1722 | ExprVector Exprs; |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1723 | CommaLocsTy CommaLocs; |
| 1724 | |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 1725 | auto RunSignatureHelp = [&]() { |
| 1726 | QualType PreferredType = Actions.ProduceConstructorSignatureHelp( |
| 1727 | getCurScope(), TypeRep.get()->getCanonicalTypeInternal(), |
| 1728 | DS.getEndLoc(), Exprs, T.getOpenLocation()); |
| 1729 | CalledSignatureHelp = true; |
| 1730 | return PreferredType; |
| 1731 | }; |
| 1732 | |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1733 | if (Tok.isNot(tok::r_paren)) { |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 1734 | if (ParseExpressionList(Exprs, CommaLocs, [&] { |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 1735 | PreferredType.enterFunctionArgument(Tok.getLocation(), |
| 1736 | RunSignatureHelp); |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1737 | })) { |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 1738 | if (PP.isCodeCompletionReached() && !CalledSignatureHelp) |
| 1739 | RunSignatureHelp(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1740 | SkipUntil(tok::r_paren, StopAtSemi); |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1741 | return ExprError(); |
| 1742 | } |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1743 | } |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1744 | |
| 1745 | // Match the ')'. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1746 | T.consumeClose(); |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1747 | |
| 1748 | // TypeRep could be null, if it references an invalid typedef. |
| 1749 | if (!TypeRep) |
| 1750 | return ExprError(); |
| 1751 | |
| 1752 | assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&& |
| 1753 | "Unexpected number of commas!"); |
Vedant Kumar | a14a1f9 | 2018-01-17 18:53:51 +0000 | [diff] [blame] | 1754 | return Actions.ActOnCXXTypeConstructExpr(TypeRep, T.getOpenLocation(), |
| 1755 | Exprs, T.getCloseLocation(), |
| 1756 | /*ListInitialization=*/false); |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1757 | } |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1758 | } |
| 1759 | |
Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 1760 | /// ParseCXXCondition - if/switch/while condition expression. |
Argyrios Kyrtzidis | 2b4072f | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1761 | /// |
| 1762 | /// condition: |
| 1763 | /// expression |
| 1764 | /// type-specifier-seq declarator '=' assignment-expression |
Richard Smith | 2a15b74 | 2012-02-22 06:49:09 +0000 | [diff] [blame] | 1765 | /// [C++11] type-specifier-seq declarator '=' initializer-clause |
| 1766 | /// [C++11] type-specifier-seq declarator braced-init-list |
Zhihao Yuan | c81f453 | 2017-12-07 07:03:15 +0000 | [diff] [blame] | 1767 | /// [Clang] type-specifier-seq ref-qualifier[opt] '[' identifier-list ']' |
| 1768 | /// brace-or-equal-initializer |
Argyrios Kyrtzidis | 2b4072f | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1769 | /// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt] |
| 1770 | /// '=' assignment-expression |
| 1771 | /// |
Richard Smith | c7a05a9 | 2016-06-29 21:17:59 +0000 | [diff] [blame] | 1772 | /// In C++1z, a condition may in some contexts be preceded by an |
| 1773 | /// optional init-statement. This function will parse that too. |
| 1774 | /// |
| 1775 | /// \param InitStmt If non-null, an init-statement is permitted, and if present |
| 1776 | /// will be parsed and stored here. |
| 1777 | /// |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1778 | /// \param Loc The location of the start of the statement that requires this |
| 1779 | /// condition, e.g., the "for" in a for loop. |
| 1780 | /// |
Richard Smith | 8baa500 | 2018-09-28 18:44:09 +0000 | [diff] [blame] | 1781 | /// \param FRI If non-null, a for range declaration is permitted, and if |
| 1782 | /// present will be parsed and stored here, and a null result will be returned. |
| 1783 | /// |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 1784 | /// \returns The parsed condition. |
Richard Smith | c7a05a9 | 2016-06-29 21:17:59 +0000 | [diff] [blame] | 1785 | Sema::ConditionResult Parser::ParseCXXCondition(StmtResult *InitStmt, |
| 1786 | SourceLocation Loc, |
Richard Smith | 8baa500 | 2018-09-28 18:44:09 +0000 | [diff] [blame] | 1787 | Sema::ConditionKind CK, |
| 1788 | ForRangeInfo *FRI) { |
Richard Smith | bf5bcf2 | 2018-06-26 23:20:26 +0000 | [diff] [blame] | 1789 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 1790 | PreferredType.enterCondition(Actions, Tok.getLocation()); |
Richard Smith | bf5bcf2 | 2018-06-26 23:20:26 +0000 | [diff] [blame] | 1791 | |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1792 | if (Tok.is(tok::code_completion)) { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1793 | Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Condition); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1794 | cutOffParsing(); |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 1795 | return Sema::ConditionError(); |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1796 | } |
| 1797 | |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1798 | ParsedAttributesWithRange attrs(AttrFactory); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1799 | MaybeParseCXX11Attributes(attrs); |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1800 | |
Zhihao Yuan | 52b5bf8 | 2018-03-17 21:42:10 +0000 | [diff] [blame] | 1801 | const auto WarnOnInit = [this, &CK] { |
| 1802 | Diag(Tok.getLocation(), getLangOpts().CPlusPlus17 |
| 1803 | ? diag::warn_cxx14_compat_init_statement |
| 1804 | : diag::ext_init_statement) |
| 1805 | << (CK == Sema::ConditionKind::Switch); |
| 1806 | }; |
| 1807 | |
Richard Smith | c7a05a9 | 2016-06-29 21:17:59 +0000 | [diff] [blame] | 1808 | // Determine what kind of thing we have. |
Richard Smith | 8baa500 | 2018-09-28 18:44:09 +0000 | [diff] [blame] | 1809 | switch (isCXXConditionDeclarationOrInitStatement(InitStmt, FRI)) { |
Richard Smith | c7a05a9 | 2016-06-29 21:17:59 +0000 | [diff] [blame] | 1810 | case ConditionOrInitStatement::Expression: { |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1811 | ProhibitAttributes(attrs); |
| 1812 | |
Zhihao Yuan | 52b5bf8 | 2018-03-17 21:42:10 +0000 | [diff] [blame] | 1813 | // We can have an empty expression here. |
| 1814 | // if (; true); |
| 1815 | if (InitStmt && Tok.is(tok::semi)) { |
| 1816 | WarnOnInit(); |
Roman Lebedev | 377748f | 2018-11-20 18:59:05 +0000 | [diff] [blame] | 1817 | SourceLocation SemiLoc = Tok.getLocation(); |
| 1818 | if (!Tok.hasLeadingEmptyMacro() && !SemiLoc.isMacroID()) { |
| 1819 | Diag(SemiLoc, diag::warn_empty_init_statement) |
| 1820 | << (CK == Sema::ConditionKind::Switch) |
| 1821 | << FixItHint::CreateRemoval(SemiLoc); |
| 1822 | } |
| 1823 | ConsumeToken(); |
Zhihao Yuan | 52b5bf8 | 2018-03-17 21:42:10 +0000 | [diff] [blame] | 1824 | *InitStmt = Actions.ActOnNullStmt(SemiLoc); |
| 1825 | return ParseCXXCondition(nullptr, Loc, CK); |
| 1826 | } |
| 1827 | |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1828 | // Parse the expression. |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 1829 | ExprResult Expr = ParseExpression(); // expression |
| 1830 | if (Expr.isInvalid()) |
| 1831 | return Sema::ConditionError(); |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1832 | |
Richard Smith | c7a05a9 | 2016-06-29 21:17:59 +0000 | [diff] [blame] | 1833 | if (InitStmt && Tok.is(tok::semi)) { |
Zhihao Yuan | 52b5bf8 | 2018-03-17 21:42:10 +0000 | [diff] [blame] | 1834 | WarnOnInit(); |
Richard Smith | c7a05a9 | 2016-06-29 21:17:59 +0000 | [diff] [blame] | 1835 | *InitStmt = Actions.ActOnExprStmt(Expr.get()); |
| 1836 | ConsumeToken(); |
| 1837 | return ParseCXXCondition(nullptr, Loc, CK); |
| 1838 | } |
| 1839 | |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 1840 | return Actions.ActOnCondition(getCurScope(), Loc, Expr.get(), CK); |
Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 1841 | } |
Argyrios Kyrtzidis | 2b4072f | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1842 | |
Richard Smith | c7a05a9 | 2016-06-29 21:17:59 +0000 | [diff] [blame] | 1843 | case ConditionOrInitStatement::InitStmtDecl: { |
Zhihao Yuan | 52b5bf8 | 2018-03-17 21:42:10 +0000 | [diff] [blame] | 1844 | WarnOnInit(); |
Richard Smith | c7a05a9 | 2016-06-29 21:17:59 +0000 | [diff] [blame] | 1845 | SourceLocation DeclStart = Tok.getLocation(), DeclEnd; |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 1846 | DeclGroupPtrTy DG = |
| 1847 | ParseSimpleDeclaration(DeclaratorContext::InitStmtContext, DeclEnd, |
| 1848 | attrs, /*RequireSemi=*/true); |
Richard Smith | c7a05a9 | 2016-06-29 21:17:59 +0000 | [diff] [blame] | 1849 | *InitStmt = Actions.ActOnDeclStmt(DG, DeclStart, DeclEnd); |
| 1850 | return ParseCXXCondition(nullptr, Loc, CK); |
| 1851 | } |
| 1852 | |
Richard Smith | 8baa500 | 2018-09-28 18:44:09 +0000 | [diff] [blame] | 1853 | case ConditionOrInitStatement::ForRangeDecl: { |
| 1854 | assert(FRI && "should not parse a for range declaration here"); |
| 1855 | SourceLocation DeclStart = Tok.getLocation(), DeclEnd; |
| 1856 | DeclGroupPtrTy DG = ParseSimpleDeclaration( |
| 1857 | DeclaratorContext::ForContext, DeclEnd, attrs, false, FRI); |
| 1858 | FRI->LoopVar = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation()); |
| 1859 | return Sema::ConditionResult(); |
| 1860 | } |
| 1861 | |
Richard Smith | c7a05a9 | 2016-06-29 21:17:59 +0000 | [diff] [blame] | 1862 | case ConditionOrInitStatement::ConditionDecl: |
| 1863 | case ConditionOrInitStatement::Error: |
| 1864 | break; |
| 1865 | } |
| 1866 | |
Argyrios Kyrtzidis | 2b4072f | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1867 | // type-specifier-seq |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 1868 | DeclSpec DS(AttrFactory); |
Richard Smith | 54ecd98 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 1869 | DS.takeAttributesFrom(attrs); |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 1870 | ParseSpecifierQualifierList(DS, AS_none, DeclSpecContext::DSC_condition); |
Argyrios Kyrtzidis | 2b4072f | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1871 | |
| 1872 | // declarator |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 1873 | Declarator DeclaratorInfo(DS, DeclaratorContext::ConditionContext); |
Argyrios Kyrtzidis | 2b4072f | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1874 | ParseDeclarator(DeclaratorInfo); |
| 1875 | |
| 1876 | // simple-asm-expr[opt] |
| 1877 | if (Tok.is(tok::kw_asm)) { |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1878 | SourceLocation Loc; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1879 | ExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1880 | if (AsmLabel.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1881 | SkipUntil(tok::semi, StopAtSemi); |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 1882 | return Sema::ConditionError(); |
Argyrios Kyrtzidis | 2b4072f | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1883 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1884 | DeclaratorInfo.setAsmLabel(AsmLabel.get()); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1885 | DeclaratorInfo.SetRangeEnd(Loc); |
Argyrios Kyrtzidis | 2b4072f | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1886 | } |
| 1887 | |
| 1888 | // If attributes are present, parse them. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1889 | MaybeParseGNUAttributes(DeclaratorInfo); |
Argyrios Kyrtzidis | 2b4072f | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1890 | |
Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 1891 | // Type-check the declaration itself. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1892 | DeclResult Dcl = Actions.ActOnCXXConditionDeclaration(getCurScope(), |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1893 | DeclaratorInfo); |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 1894 | if (Dcl.isInvalid()) |
| 1895 | return Sema::ConditionError(); |
| 1896 | Decl *DeclOut = Dcl.get(); |
Argyrios Kyrtzidis | b5c7c51 | 2010-10-08 02:39:23 +0000 | [diff] [blame] | 1897 | |
Argyrios Kyrtzidis | 2b4072f | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1898 | // '=' assignment-expression |
Richard Trieu | c64d323 | 2012-01-18 22:54:52 +0000 | [diff] [blame] | 1899 | // If a '==' or '+=' is found, suggest a fixit to '='. |
Richard Smith | 2a15b74 | 2012-02-22 06:49:09 +0000 | [diff] [blame] | 1900 | bool CopyInitialization = isTokenEqualOrEqualTypo(); |
| 1901 | if (CopyInitialization) |
Jeffrey Yasskin | 8dfa5f1 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 1902 | ConsumeToken(); |
Richard Smith | 2a15b74 | 2012-02-22 06:49:09 +0000 | [diff] [blame] | 1903 | |
| 1904 | ExprResult InitExpr = ExprError(); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1905 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) { |
Richard Smith | 2a15b74 | 2012-02-22 06:49:09 +0000 | [diff] [blame] | 1906 | Diag(Tok.getLocation(), |
| 1907 | diag::warn_cxx98_compat_generalized_initializer_lists); |
| 1908 | InitExpr = ParseBraceInitializer(); |
| 1909 | } else if (CopyInitialization) { |
Ilya Biryukov | 4f9543b | 2019-01-31 20:20:32 +0000 | [diff] [blame] | 1910 | PreferredType.enterVariableInit(Tok.getLocation(), DeclOut); |
Richard Smith | 2a15b74 | 2012-02-22 06:49:09 +0000 | [diff] [blame] | 1911 | InitExpr = ParseAssignmentExpression(); |
| 1912 | } else if (Tok.is(tok::l_paren)) { |
| 1913 | // This was probably an attempt to initialize the variable. |
| 1914 | SourceLocation LParen = ConsumeParen(), RParen = LParen; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1915 | if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch)) |
Richard Smith | 2a15b74 | 2012-02-22 06:49:09 +0000 | [diff] [blame] | 1916 | RParen = ConsumeParen(); |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 1917 | Diag(DeclOut->getLocation(), |
Richard Smith | 2a15b74 | 2012-02-22 06:49:09 +0000 | [diff] [blame] | 1918 | diag::err_expected_init_in_condition_lparen) |
| 1919 | << SourceRange(LParen, RParen); |
Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 1920 | } else { |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 1921 | Diag(DeclOut->getLocation(), diag::err_expected_init_in_condition); |
Douglas Gregor | 7bab5ff | 2009-11-25 00:27:52 +0000 | [diff] [blame] | 1922 | } |
Richard Smith | 2a15b74 | 2012-02-22 06:49:09 +0000 | [diff] [blame] | 1923 | |
| 1924 | if (!InitExpr.isInvalid()) |
Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 1925 | Actions.AddInitializerToDecl(DeclOut, InitExpr.get(), !CopyInitialization); |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 1926 | else |
| 1927 | Actions.ActOnInitializerError(DeclOut); |
Richard Smith | 2a15b74 | 2012-02-22 06:49:09 +0000 | [diff] [blame] | 1928 | |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 1929 | Actions.FinalizeDeclaration(DeclOut); |
Richard Smith | 03a4aa3 | 2016-06-23 19:02:52 +0000 | [diff] [blame] | 1930 | return Actions.ActOnConditionVariable(DeclOut, Loc, CK); |
Argyrios Kyrtzidis | 2b4072f | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 1931 | } |
| 1932 | |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1933 | /// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers. |
| 1934 | /// This should only be called when the current token is known to be part of |
| 1935 | /// simple-type-specifier. |
| 1936 | /// |
| 1937 | /// simple-type-specifier: |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1938 | /// '::'[opt] nested-name-specifier[opt] type-name |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1939 | /// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO] |
| 1940 | /// char |
| 1941 | /// wchar_t |
| 1942 | /// bool |
| 1943 | /// short |
| 1944 | /// int |
| 1945 | /// long |
| 1946 | /// signed |
| 1947 | /// unsigned |
| 1948 | /// float |
| 1949 | /// double |
| 1950 | /// void |
| 1951 | /// [GNU] typeof-specifier |
| 1952 | /// [C++0x] auto [TODO] |
| 1953 | /// |
| 1954 | /// type-name: |
| 1955 | /// class-name |
| 1956 | /// enum-name |
| 1957 | /// typedef-name |
| 1958 | /// |
| 1959 | void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) { |
| 1960 | DS.SetRangeStart(Tok.getLocation()); |
| 1961 | const char *PrevSpec; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1962 | unsigned DiagID; |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1963 | SourceLocation Loc = Tok.getLocation(); |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 1964 | const clang::PrintingPolicy &Policy = |
| 1965 | Actions.getASTContext().getPrintingPolicy(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1966 | |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1967 | switch (Tok.getKind()) { |
Chris Lattner | 45ddec3 | 2009-01-05 00:13:00 +0000 | [diff] [blame] | 1968 | case tok::identifier: // foo::bar |
| 1969 | case tok::coloncolon: // ::foo::bar |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1970 | llvm_unreachable("Annotation token should already be formed!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1971 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1972 | llvm_unreachable("Not a simple-type-specifier token!"); |
Chris Lattner | 45ddec3 | 2009-01-05 00:13:00 +0000 | [diff] [blame] | 1973 | |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1974 | // type-name |
Chris Lattner | a8a3f73 | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 1975 | case tok::annot_typename: { |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1976 | if (getTypeAnnotation(Tok)) |
| 1977 | DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 1978 | getTypeAnnotation(Tok), Policy); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1979 | else |
| 1980 | DS.SetTypeSpecError(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1981 | |
Douglas Gregor | 06e41ae | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 1982 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 1983 | ConsumeAnnotationToken(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1984 | |
Craig Topper | 2512241 | 2015-11-15 03:32:11 +0000 | [diff] [blame] | 1985 | DS.Finish(Actions, Policy); |
Douglas Gregor | 06e41ae | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 1986 | return; |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1987 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1988 | |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1989 | // builtin types |
| 1990 | case tok::kw_short: |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 1991 | DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID, Policy); |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1992 | break; |
| 1993 | case tok::kw_long: |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 1994 | DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID, Policy); |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1995 | break; |
Francois Pichet | 84133e4 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 1996 | case tok::kw___int64: |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 1997 | DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, DiagID, Policy); |
Francois Pichet | 84133e4 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 1998 | break; |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1999 | case tok::kw_signed: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2000 | DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID); |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 2001 | break; |
| 2002 | case tok::kw_unsigned: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2003 | DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID); |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 2004 | break; |
| 2005 | case tok::kw_void: |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2006 | DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID, Policy); |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 2007 | break; |
| 2008 | case tok::kw_char: |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2009 | DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID, Policy); |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 2010 | break; |
| 2011 | case tok::kw_int: |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2012 | DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID, Policy); |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 2013 | break; |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 2014 | case tok::kw___int128: |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2015 | DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, DiagID, Policy); |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 2016 | break; |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 2017 | case tok::kw_half: |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2018 | DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID, Policy); |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 2019 | break; |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 2020 | case tok::kw_float: |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2021 | DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID, Policy); |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 2022 | break; |
| 2023 | case tok::kw_double: |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2024 | DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID, Policy); |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 2025 | break; |
Sjoerd Meijer | cc623ad | 2017-09-08 15:15:00 +0000 | [diff] [blame] | 2026 | case tok::kw__Float16: |
| 2027 | DS.SetTypeSpecType(DeclSpec::TST_float16, Loc, PrevSpec, DiagID, Policy); |
| 2028 | break; |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 2029 | case tok::kw___float128: |
| 2030 | DS.SetTypeSpecType(DeclSpec::TST_float128, Loc, PrevSpec, DiagID, Policy); |
| 2031 | break; |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 2032 | case tok::kw_wchar_t: |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2033 | DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID, Policy); |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 2034 | break; |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 2035 | case tok::kw_char8_t: |
| 2036 | DS.SetTypeSpecType(DeclSpec::TST_char8, Loc, PrevSpec, DiagID, Policy); |
| 2037 | break; |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2038 | case tok::kw_char16_t: |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2039 | DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID, Policy); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2040 | break; |
| 2041 | case tok::kw_char32_t: |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2042 | DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID, Policy); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2043 | break; |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 2044 | case tok::kw_bool: |
Erik Verbruggen | 888d52a | 2014-01-15 09:15:43 +0000 | [diff] [blame] | 2045 | DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID, Policy); |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 2046 | break; |
Anastasia Stulova | 2c4730d | 2019-02-15 12:07:57 +0000 | [diff] [blame] | 2047 | #define GENERIC_IMAGE_TYPE(ImgType, Id) \ |
| 2048 | case tok::kw_##ImgType##_t: \ |
| 2049 | DS.SetTypeSpecType(DeclSpec::TST_##ImgType##_t, Loc, PrevSpec, DiagID, \ |
| 2050 | Policy); \ |
| 2051 | break; |
| 2052 | #include "clang/Basic/OpenCLImageTypes.def" |
| 2053 | |
David Blaikie | 25896afb | 2012-01-24 05:47:35 +0000 | [diff] [blame] | 2054 | case tok::annot_decltype: |
| 2055 | case tok::kw_decltype: |
| 2056 | DS.SetRangeEnd(ParseDecltypeSpecifier(DS)); |
Craig Topper | 2512241 | 2015-11-15 03:32:11 +0000 | [diff] [blame] | 2057 | return DS.Finish(Actions, Policy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2058 | |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 2059 | // GNU typeof support. |
| 2060 | case tok::kw_typeof: |
| 2061 | ParseTypeofSpecifier(DS); |
Craig Topper | 2512241 | 2015-11-15 03:32:11 +0000 | [diff] [blame] | 2062 | DS.Finish(Actions, Policy); |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 2063 | return; |
| 2064 | } |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 2065 | ConsumeAnyToken(); |
| 2066 | DS.SetRangeEnd(PrevTokLocation); |
Craig Topper | 2512241 | 2015-11-15 03:32:11 +0000 | [diff] [blame] | 2067 | DS.Finish(Actions, Policy); |
Argyrios Kyrtzidis | 857fcc2 | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 2068 | } |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 2069 | |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2070 | /// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++ |
| 2071 | /// [dcl.name]), which is a non-empty sequence of type-specifiers, |
| 2072 | /// e.g., "const short int". Note that the DeclSpec is *not* finished |
| 2073 | /// by parsing the type-specifier-seq, because these sequences are |
| 2074 | /// typically followed by some form of declarator. Returns true and |
| 2075 | /// emits diagnostics if this is not a type-specifier-seq, false |
| 2076 | /// otherwise. |
| 2077 | /// |
| 2078 | /// type-specifier-seq: [C++ 8.1] |
| 2079 | /// type-specifier type-specifier-seq[opt] |
| 2080 | /// |
| 2081 | bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) { |
Faisal Vali | 7db85c5 | 2017-12-31 00:06:40 +0000 | [diff] [blame] | 2082 | ParseSpecifierQualifierList(DS, AS_none, DeclSpecContext::DSC_type_specifier); |
Craig Topper | 2512241 | 2015-11-15 03:32:11 +0000 | [diff] [blame] | 2083 | DS.Finish(Actions, Actions.getASTContext().getPrintingPolicy()); |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2084 | return false; |
| 2085 | } |
| 2086 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2087 | /// Finish parsing a C++ unqualified-id that is a template-id of |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2088 | /// some form. |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2089 | /// |
| 2090 | /// This routine is invoked when a '<' is encountered after an identifier or |
| 2091 | /// operator-function-id is parsed by \c ParseUnqualifiedId() to determine |
| 2092 | /// whether the unqualified-id is actually a template-id. This routine will |
| 2093 | /// then parse the template arguments and form the appropriate template-id to |
| 2094 | /// return to the caller. |
| 2095 | /// |
| 2096 | /// \param SS the nested-name-specifier that precedes this template-id, if |
| 2097 | /// we're actually parsing a qualified-id. |
| 2098 | /// |
| 2099 | /// \param Name for constructor and destructor names, this is the actual |
| 2100 | /// identifier that may be a template-name. |
| 2101 | /// |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2102 | /// \param NameLoc the location of the class-name in a constructor or |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2103 | /// destructor. |
| 2104 | /// |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2105 | /// \param EnteringContext whether we're entering the scope of the |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2106 | /// nested-name-specifier. |
| 2107 | /// |
Douglas Gregor | 127ea59 | 2009-11-03 21:24:04 +0000 | [diff] [blame] | 2108 | /// \param ObjectType if this unqualified-id occurs within a member access |
| 2109 | /// expression, the type of the base object whose member is being accessed. |
| 2110 | /// |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2111 | /// \param Id as input, describes the template-name or operator-function-id |
| 2112 | /// that precedes the '<'. If template arguments were parsed successfully, |
| 2113 | /// will be updated with the template-id. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2114 | /// |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 2115 | /// \param AssumeTemplateId When true, this routine will assume that the name |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2116 | /// refers to a template without performing name lookup to verify. |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 2117 | /// |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2118 | /// \returns true if a parse error occurred, false otherwise. |
| 2119 | bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2120 | SourceLocation TemplateKWLoc, |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2121 | IdentifierInfo *Name, |
| 2122 | SourceLocation NameLoc, |
| 2123 | bool EnteringContext, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2124 | ParsedType ObjectType, |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 2125 | UnqualifiedId &Id, |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2126 | bool AssumeTemplateId) { |
Richard Smith | c08b693 | 2018-04-27 02:00:13 +0000 | [diff] [blame] | 2127 | assert(Tok.is(tok::less) && "Expected '<' to finish parsing a template-id"); |
| 2128 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2129 | TemplateTy Template; |
| 2130 | TemplateNameKind TNK = TNK_Non_template; |
| 2131 | switch (Id.getKind()) { |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 2132 | case UnqualifiedIdKind::IK_Identifier: |
| 2133 | case UnqualifiedIdKind::IK_OperatorFunctionId: |
| 2134 | case UnqualifiedIdKind::IK_LiteralOperatorId: |
Douglas Gregor | e610ada | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 2135 | if (AssumeTemplateId) { |
Richard Smith | fd3dae0 | 2017-01-20 00:20:39 +0000 | [diff] [blame] | 2136 | // We defer the injected-class-name checks until we've found whether |
| 2137 | // this template-id is used to form a nested-name-specifier or not. |
| 2138 | TNK = Actions.ActOnDependentTemplateName( |
| 2139 | getCurScope(), SS, TemplateKWLoc, Id, ObjectType, EnteringContext, |
| 2140 | Template, /*AllowInjectedClassName*/ true); |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2141 | if (TNK == TNK_Non_template) |
| 2142 | return true; |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2143 | } else { |
| 2144 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 2145 | TNK = Actions.isTemplateName(getCurScope(), SS, |
| 2146 | TemplateKWLoc.isValid(), Id, |
| 2147 | ObjectType, EnteringContext, Template, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2148 | MemberOfUnknownSpecialization); |
Richard Smith | b23c5e8 | 2019-05-09 03:31:27 +0000 | [diff] [blame] | 2149 | // If lookup found nothing but we're assuming that this is a template |
| 2150 | // name, double-check that makes sense syntactically before committing |
| 2151 | // to it. |
| 2152 | if (TNK == TNK_Undeclared_template && |
| 2153 | isTemplateArgumentList(0) == TPResult::False) |
| 2154 | return false; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2155 | |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2156 | if (TNK == TNK_Non_template && MemberOfUnknownSpecialization && |
Richard Smith | b23c5e8 | 2019-05-09 03:31:27 +0000 | [diff] [blame] | 2157 | ObjectType && isTemplateArgumentList(0) == TPResult::True) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2158 | // We have something like t->getAs<T>(), where getAs is a |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2159 | // member of an unknown specialization. However, this will only |
| 2160 | // parse correctly as a template, so suggest the keyword 'template' |
| 2161 | // before 'getAs' and treat this as a dependent template name. |
| 2162 | std::string Name; |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 2163 | if (Id.getKind() == UnqualifiedIdKind::IK_Identifier) |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2164 | Name = Id.Identifier->getName(); |
| 2165 | else { |
| 2166 | Name = "operator "; |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 2167 | if (Id.getKind() == UnqualifiedIdKind::IK_OperatorFunctionId) |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2168 | Name += getOperatorSpelling(Id.OperatorFunctionId.Operator); |
| 2169 | else |
| 2170 | Name += Id.Identifier->getName(); |
| 2171 | } |
| 2172 | Diag(Id.StartLocation, diag::err_missing_dependent_template_keyword) |
| 2173 | << Name |
| 2174 | << FixItHint::CreateInsertion(Id.StartLocation, "template "); |
Richard Smith | fd3dae0 | 2017-01-20 00:20:39 +0000 | [diff] [blame] | 2175 | TNK = Actions.ActOnDependentTemplateName( |
| 2176 | getCurScope(), SS, TemplateKWLoc, Id, ObjectType, EnteringContext, |
| 2177 | Template, /*AllowInjectedClassName*/ true); |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2178 | if (TNK == TNK_Non_template) |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2179 | return true; |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2180 | } |
| 2181 | } |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2182 | break; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2183 | |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 2184 | case UnqualifiedIdKind::IK_ConstructorName: { |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2185 | UnqualifiedId TemplateName; |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2186 | bool MemberOfUnknownSpecialization; |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2187 | TemplateName.setIdentifier(Name, NameLoc); |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 2188 | TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(), |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2189 | TemplateName, ObjectType, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2190 | EnteringContext, Template, |
| 2191 | MemberOfUnknownSpecialization); |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2192 | break; |
| 2193 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2194 | |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 2195 | case UnqualifiedIdKind::IK_DestructorName: { |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2196 | UnqualifiedId TemplateName; |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2197 | bool MemberOfUnknownSpecialization; |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2198 | TemplateName.setIdentifier(Name, NameLoc); |
Douglas Gregor | 30d60cb | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 2199 | if (ObjectType) { |
Richard Smith | fd3dae0 | 2017-01-20 00:20:39 +0000 | [diff] [blame] | 2200 | TNK = Actions.ActOnDependentTemplateName( |
| 2201 | getCurScope(), SS, TemplateKWLoc, TemplateName, ObjectType, |
| 2202 | EnteringContext, Template, /*AllowInjectedClassName*/ true); |
Douglas Gregor | bb11965 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2203 | if (TNK == TNK_Non_template) |
Douglas Gregor | 30d60cb | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 2204 | return true; |
| 2205 | } else { |
Abramo Bagnara | 7c5dee4 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 2206 | TNK = Actions.isTemplateName(getCurScope(), SS, TemplateKWLoc.isValid(), |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2207 | TemplateName, ObjectType, |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2208 | EnteringContext, Template, |
| 2209 | MemberOfUnknownSpecialization); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2210 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2211 | if (TNK == TNK_Non_template && !Id.DestructorName.get()) { |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 2212 | Diag(NameLoc, diag::err_destructor_template_id) |
| 2213 | << Name << SS.getRange(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2214 | return true; |
Douglas Gregor | 30d60cb | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 2215 | } |
| 2216 | } |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2217 | break; |
Douglas Gregor | 3cf8131 | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2218 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2219 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2220 | default: |
| 2221 | return false; |
| 2222 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2223 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2224 | if (TNK == TNK_Non_template) |
| 2225 | return false; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2226 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2227 | // Parse the enclosed template argument list. |
| 2228 | SourceLocation LAngleLoc, RAngleLoc; |
| 2229 | TemplateArgList TemplateArgs; |
Richard Smith | c08b693 | 2018-04-27 02:00:13 +0000 | [diff] [blame] | 2230 | if (ParseTemplateIdAfterTemplateName(true, LAngleLoc, TemplateArgs, |
| 2231 | RAngleLoc)) |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2232 | return true; |
Richard Smith | c08b693 | 2018-04-27 02:00:13 +0000 | [diff] [blame] | 2233 | |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 2234 | if (Id.getKind() == UnqualifiedIdKind::IK_Identifier || |
| 2235 | Id.getKind() == UnqualifiedIdKind::IK_OperatorFunctionId || |
| 2236 | Id.getKind() == UnqualifiedIdKind::IK_LiteralOperatorId) { |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2237 | // Form a parsed representation of the template-id to be stored in the |
| 2238 | // UnqualifiedId. |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2239 | |
Richard Smith | 72bfbd8 | 2013-12-04 00:28:23 +0000 | [diff] [blame] | 2240 | // FIXME: Store name for literal operator too. |
Faisal Vali | 43caf67 | 2017-05-23 01:07:12 +0000 | [diff] [blame] | 2241 | IdentifierInfo *TemplateII = |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 2242 | Id.getKind() == UnqualifiedIdKind::IK_Identifier ? Id.Identifier |
| 2243 | : nullptr; |
| 2244 | OverloadedOperatorKind OpKind = |
| 2245 | Id.getKind() == UnqualifiedIdKind::IK_Identifier |
| 2246 | ? OO_None |
| 2247 | : Id.OperatorFunctionId.Operator; |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2248 | |
Faisal Vali | 43caf67 | 2017-05-23 01:07:12 +0000 | [diff] [blame] | 2249 | TemplateIdAnnotation *TemplateId = TemplateIdAnnotation::Create( |
| 2250 | SS, TemplateKWLoc, Id.StartLocation, TemplateII, OpKind, Template, TNK, |
| 2251 | LAngleLoc, RAngleLoc, TemplateArgs, TemplateIds); |
| 2252 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2253 | Id.setTemplateId(TemplateId); |
| 2254 | return false; |
| 2255 | } |
| 2256 | |
| 2257 | // Bundle the template arguments together. |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 2258 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateArgs); |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2259 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2260 | // Constructor and destructor names. |
Richard Smith | b23c5e8 | 2019-05-09 03:31:27 +0000 | [diff] [blame] | 2261 | TypeResult Type = Actions.ActOnTemplateIdType( |
| 2262 | getCurScope(), SS, TemplateKWLoc, Template, Name, NameLoc, LAngleLoc, |
| 2263 | TemplateArgsPtr, RAngleLoc, /*IsCtorOrDtorName=*/true); |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2264 | if (Type.isInvalid()) |
| 2265 | return true; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2266 | |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 2267 | if (Id.getKind() == UnqualifiedIdKind::IK_ConstructorName) |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2268 | Id.setConstructorName(Type.get(), NameLoc, RAngleLoc); |
| 2269 | else |
| 2270 | Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2271 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2272 | return false; |
| 2273 | } |
| 2274 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2275 | /// Parse an operator-function-id or conversion-function-id as part |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2276 | /// of a C++ unqualified-id. |
| 2277 | /// |
| 2278 | /// This routine is responsible only for parsing the operator-function-id or |
| 2279 | /// conversion-function-id; it does not handle template arguments in any way. |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2280 | /// |
| 2281 | /// \code |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2282 | /// operator-function-id: [C++ 13.5] |
| 2283 | /// 'operator' operator |
| 2284 | /// |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2285 | /// operator: one of |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2286 | /// new delete new[] delete[] |
| 2287 | /// + - * / % ^ & | ~ |
| 2288 | /// ! = < > += -= *= /= %= |
| 2289 | /// ^= &= |= << >> >>= <<= == != |
| 2290 | /// <= >= && || ++ -- , ->* -> |
Richard Smith | d30b23d | 2017-12-01 02:13:10 +0000 | [diff] [blame] | 2291 | /// () [] <=> |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2292 | /// |
| 2293 | /// conversion-function-id: [C++ 12.3.2] |
| 2294 | /// operator conversion-type-id |
| 2295 | /// |
| 2296 | /// conversion-type-id: |
| 2297 | /// type-specifier-seq conversion-declarator[opt] |
| 2298 | /// |
| 2299 | /// conversion-declarator: |
| 2300 | /// ptr-operator conversion-declarator[opt] |
| 2301 | /// \endcode |
| 2302 | /// |
Dmitri Gribenko | dd28e79 | 2012-08-24 00:01:24 +0000 | [diff] [blame] | 2303 | /// \param SS The nested-name-specifier that preceded this unqualified-id. If |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2304 | /// non-empty, then we are parsing the unqualified-id of a qualified-id. |
| 2305 | /// |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2306 | /// \param EnteringContext whether we are entering the scope of the |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2307 | /// nested-name-specifier. |
| 2308 | /// |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2309 | /// \param ObjectType if this unqualified-id occurs within a member access |
| 2310 | /// expression, the type of the base object whose member is being accessed. |
| 2311 | /// |
| 2312 | /// \param Result on a successful parse, contains the parsed unqualified-id. |
| 2313 | /// |
| 2314 | /// \returns true if parsing fails, false otherwise. |
| 2315 | bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2316 | ParsedType ObjectType, |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2317 | UnqualifiedId &Result) { |
| 2318 | assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2319 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2320 | // Consume the 'operator' keyword. |
| 2321 | SourceLocation KeywordLoc = ConsumeToken(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2322 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2323 | // Determine what kind of operator name we have. |
| 2324 | unsigned SymbolIdx = 0; |
| 2325 | SourceLocation SymbolLocations[3]; |
| 2326 | OverloadedOperatorKind Op = OO_None; |
| 2327 | switch (Tok.getKind()) { |
| 2328 | case tok::kw_new: |
| 2329 | case tok::kw_delete: { |
| 2330 | bool isNew = Tok.getKind() == tok::kw_new; |
| 2331 | // Consume the 'new' or 'delete'. |
| 2332 | SymbolLocations[SymbolIdx++] = ConsumeToken(); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 2333 | // Check for array new/delete. |
| 2334 | if (Tok.is(tok::l_square) && |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2335 | (!getLangOpts().CPlusPlus11 || NextToken().isNot(tok::l_square))) { |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2336 | // Consume the '[' and ']'. |
| 2337 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 2338 | T.consumeOpen(); |
| 2339 | T.consumeClose(); |
| 2340 | if (T.getCloseLocation().isInvalid()) |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2341 | return true; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2342 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2343 | SymbolLocations[SymbolIdx++] = T.getOpenLocation(); |
| 2344 | SymbolLocations[SymbolIdx++] = T.getCloseLocation(); |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2345 | Op = isNew? OO_Array_New : OO_Array_Delete; |
| 2346 | } else { |
| 2347 | Op = isNew? OO_New : OO_Delete; |
| 2348 | } |
| 2349 | break; |
| 2350 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2351 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2352 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 2353 | case tok::Token: \ |
| 2354 | SymbolLocations[SymbolIdx++] = ConsumeToken(); \ |
| 2355 | Op = OO_##Name; \ |
| 2356 | break; |
| 2357 | #define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly) |
| 2358 | #include "clang/Basic/OperatorKinds.def" |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2359 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2360 | case tok::l_paren: { |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2361 | // Consume the '(' and ')'. |
| 2362 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 2363 | T.consumeOpen(); |
| 2364 | T.consumeClose(); |
| 2365 | if (T.getCloseLocation().isInvalid()) |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2366 | return true; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2367 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2368 | SymbolLocations[SymbolIdx++] = T.getOpenLocation(); |
| 2369 | SymbolLocations[SymbolIdx++] = T.getCloseLocation(); |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2370 | Op = OO_Call; |
| 2371 | break; |
| 2372 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2373 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2374 | case tok::l_square: { |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2375 | // Consume the '[' and ']'. |
| 2376 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 2377 | T.consumeOpen(); |
| 2378 | T.consumeClose(); |
| 2379 | if (T.getCloseLocation().isInvalid()) |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2380 | return true; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2381 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2382 | SymbolLocations[SymbolIdx++] = T.getOpenLocation(); |
| 2383 | SymbolLocations[SymbolIdx++] = T.getCloseLocation(); |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2384 | Op = OO_Subscript; |
| 2385 | break; |
| 2386 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2387 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2388 | case tok::code_completion: { |
| 2389 | // Code completion for the operator name. |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2390 | Actions.CodeCompleteOperatorName(getCurScope()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2391 | cutOffParsing(); |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2392 | // Don't try to parse any further. |
| 2393 | return true; |
| 2394 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2395 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2396 | default: |
| 2397 | break; |
| 2398 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2399 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2400 | if (Op != OO_None) { |
| 2401 | // We have parsed an operator-function-id. |
| 2402 | Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations); |
| 2403 | return false; |
| 2404 | } |
Alexis Hunt | 3445850 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 2405 | |
| 2406 | // Parse a literal-operator-id. |
| 2407 | // |
Richard Smith | 6f21206 | 2012-10-20 08:41:10 +0000 | [diff] [blame] | 2408 | // literal-operator-id: C++11 [over.literal] |
| 2409 | // operator string-literal identifier |
| 2410 | // operator user-defined-string-literal |
Alexis Hunt | 3445850 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 2411 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2412 | if (getLangOpts().CPlusPlus11 && isTokenStringLiteral()) { |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 2413 | Diag(Tok.getLocation(), diag::warn_cxx98_compat_literal_operator); |
Alexis Hunt | 3445850 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 2414 | |
Richard Smith | 7d182a7 | 2012-03-08 23:06:02 +0000 | [diff] [blame] | 2415 | SourceLocation DiagLoc; |
| 2416 | unsigned DiagId = 0; |
| 2417 | |
| 2418 | // We're past translation phase 6, so perform string literal concatenation |
| 2419 | // before checking for "". |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2420 | SmallVector<Token, 4> Toks; |
| 2421 | SmallVector<SourceLocation, 4> TokLocs; |
Richard Smith | 7d182a7 | 2012-03-08 23:06:02 +0000 | [diff] [blame] | 2422 | while (isTokenStringLiteral()) { |
| 2423 | if (!Tok.is(tok::string_literal) && !DiagId) { |
Richard Smith | 6f21206 | 2012-10-20 08:41:10 +0000 | [diff] [blame] | 2424 | // C++11 [over.literal]p1: |
| 2425 | // The string-literal or user-defined-string-literal in a |
| 2426 | // literal-operator-id shall have no encoding-prefix [...]. |
Richard Smith | 7d182a7 | 2012-03-08 23:06:02 +0000 | [diff] [blame] | 2427 | DiagLoc = Tok.getLocation(); |
| 2428 | DiagId = diag::err_literal_operator_string_prefix; |
| 2429 | } |
| 2430 | Toks.push_back(Tok); |
| 2431 | TokLocs.push_back(ConsumeStringToken()); |
| 2432 | } |
| 2433 | |
Craig Topper | 9d5583e | 2014-06-26 04:58:39 +0000 | [diff] [blame] | 2434 | StringLiteralParser Literal(Toks, PP); |
Richard Smith | 7d182a7 | 2012-03-08 23:06:02 +0000 | [diff] [blame] | 2435 | if (Literal.hadError) |
| 2436 | return true; |
| 2437 | |
| 2438 | // Grab the literal operator's suffix, which will be either the next token |
| 2439 | // or a ud-suffix from the string literal. |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2440 | IdentifierInfo *II = nullptr; |
Richard Smith | 7d182a7 | 2012-03-08 23:06:02 +0000 | [diff] [blame] | 2441 | SourceLocation SuffixLoc; |
| 2442 | if (!Literal.getUDSuffix().empty()) { |
| 2443 | II = &PP.getIdentifierTable().get(Literal.getUDSuffix()); |
| 2444 | SuffixLoc = |
| 2445 | Lexer::AdvanceToTokenCharacter(TokLocs[Literal.getUDSuffixToken()], |
| 2446 | Literal.getUDSuffixOffset(), |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2447 | PP.getSourceManager(), getLangOpts()); |
Richard Smith | 7d182a7 | 2012-03-08 23:06:02 +0000 | [diff] [blame] | 2448 | } else if (Tok.is(tok::identifier)) { |
| 2449 | II = Tok.getIdentifierInfo(); |
| 2450 | SuffixLoc = ConsumeToken(); |
| 2451 | TokLocs.push_back(SuffixLoc); |
| 2452 | } else { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 2453 | Diag(Tok.getLocation(), diag::err_expected) << tok::identifier; |
Alexis Hunt | 3445850 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 2454 | return true; |
| 2455 | } |
| 2456 | |
Richard Smith | 7d182a7 | 2012-03-08 23:06:02 +0000 | [diff] [blame] | 2457 | // The string literal must be empty. |
| 2458 | if (!Literal.GetString().empty() || Literal.Pascal) { |
Richard Smith | 6f21206 | 2012-10-20 08:41:10 +0000 | [diff] [blame] | 2459 | // C++11 [over.literal]p1: |
| 2460 | // The string-literal or user-defined-string-literal in a |
| 2461 | // literal-operator-id shall [...] contain no characters |
| 2462 | // other than the implicit terminating '\0'. |
Richard Smith | 7d182a7 | 2012-03-08 23:06:02 +0000 | [diff] [blame] | 2463 | DiagLoc = TokLocs.front(); |
| 2464 | DiagId = diag::err_literal_operator_string_not_empty; |
| 2465 | } |
| 2466 | |
| 2467 | if (DiagId) { |
| 2468 | // This isn't a valid literal-operator-id, but we think we know |
| 2469 | // what the user meant. Tell them what they should have written. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2470 | SmallString<32> Str; |
Richard Smith | e87aeb3 | 2015-10-08 00:17:59 +0000 | [diff] [blame] | 2471 | Str += "\"\""; |
Richard Smith | 7d182a7 | 2012-03-08 23:06:02 +0000 | [diff] [blame] | 2472 | Str += II->getName(); |
| 2473 | Diag(DiagLoc, DiagId) << FixItHint::CreateReplacement( |
| 2474 | SourceRange(TokLocs.front(), TokLocs.back()), Str); |
| 2475 | } |
| 2476 | |
| 2477 | Result.setLiteralOperatorId(II, KeywordLoc, SuffixLoc); |
Richard Smith | d091dc1 | 2013-12-05 00:58:33 +0000 | [diff] [blame] | 2478 | |
| 2479 | return Actions.checkLiteralOperatorId(SS, Result); |
Alexis Hunt | 3445850 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 2480 | } |
Richard Smith | d091dc1 | 2013-12-05 00:58:33 +0000 | [diff] [blame] | 2481 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2482 | // Parse a conversion-function-id. |
| 2483 | // |
| 2484 | // conversion-function-id: [C++ 12.3.2] |
| 2485 | // operator conversion-type-id |
| 2486 | // |
| 2487 | // conversion-type-id: |
| 2488 | // type-specifier-seq conversion-declarator[opt] |
| 2489 | // |
| 2490 | // conversion-declarator: |
| 2491 | // ptr-operator conversion-declarator[opt] |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2492 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2493 | // Parse the type-specifier-seq. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2494 | DeclSpec DS(AttrFactory); |
Douglas Gregor | a25d65d | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 2495 | if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType? |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2496 | return true; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2497 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2498 | // Parse the conversion-declarator, which is merely a sequence of |
| 2499 | // ptr-operators. |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 2500 | Declarator D(DS, DeclaratorContext::ConversionIdContext); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2501 | ParseDeclaratorInternal(D, /*DirectDeclParser=*/nullptr); |
| 2502 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2503 | // Finish up the type. |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2504 | TypeResult Ty = Actions.ActOnTypeName(getCurScope(), D); |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2505 | if (Ty.isInvalid()) |
| 2506 | return true; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2507 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2508 | // Note that this is a conversion-function-id. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2509 | Result.setConversionFunctionId(KeywordLoc, Ty.get(), |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2510 | D.getSourceRange().getEnd()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2511 | return false; |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2512 | } |
| 2513 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2514 | /// Parse a C++ unqualified-id (or a C identifier), which describes the |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2515 | /// name of an entity. |
| 2516 | /// |
| 2517 | /// \code |
| 2518 | /// unqualified-id: [C++ expr.prim.general] |
| 2519 | /// identifier |
| 2520 | /// operator-function-id |
| 2521 | /// conversion-function-id |
| 2522 | /// [C++0x] literal-operator-id [TODO] |
| 2523 | /// ~ class-name |
| 2524 | /// template-id |
| 2525 | /// |
| 2526 | /// \endcode |
| 2527 | /// |
Dmitri Gribenko | dd28e79 | 2012-08-24 00:01:24 +0000 | [diff] [blame] | 2528 | /// \param SS The nested-name-specifier that preceded this unqualified-id. If |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2529 | /// non-empty, then we are parsing the unqualified-id of a qualified-id. |
| 2530 | /// |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2531 | /// \param EnteringContext whether we are entering the scope of the |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2532 | /// nested-name-specifier. |
| 2533 | /// |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2534 | /// \param AllowDestructorName whether we allow parsing of a destructor name. |
| 2535 | /// |
| 2536 | /// \param AllowConstructorName whether we allow parsing a constructor name. |
| 2537 | /// |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 2538 | /// \param AllowDeductionGuide whether we allow parsing a deduction guide name. |
| 2539 | /// |
Douglas Gregor | 127ea59 | 2009-11-03 21:24:04 +0000 | [diff] [blame] | 2540 | /// \param ObjectType if this unqualified-id occurs within a member access |
| 2541 | /// expression, the type of the base object whose member is being accessed. |
| 2542 | /// |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2543 | /// \param Result on a successful parse, contains the parsed unqualified-id. |
| 2544 | /// |
| 2545 | /// \returns true if parsing fails, false otherwise. |
| 2546 | bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext, |
| 2547 | bool AllowDestructorName, |
| 2548 | bool AllowConstructorName, |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 2549 | bool AllowDeductionGuide, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2550 | ParsedType ObjectType, |
Richard Smith | c08b693 | 2018-04-27 02:00:13 +0000 | [diff] [blame] | 2551 | SourceLocation *TemplateKWLoc, |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2552 | UnqualifiedId &Result) { |
Richard Smith | c08b693 | 2018-04-27 02:00:13 +0000 | [diff] [blame] | 2553 | if (TemplateKWLoc) |
| 2554 | *TemplateKWLoc = SourceLocation(); |
Douglas Gregor | b22ee88 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2555 | |
| 2556 | // Handle 'A::template B'. This is for template-ids which have not |
| 2557 | // already been annotated by ParseOptionalCXXScopeSpecifier(). |
| 2558 | bool TemplateSpecified = false; |
Richard Smith | c08b693 | 2018-04-27 02:00:13 +0000 | [diff] [blame] | 2559 | if (Tok.is(tok::kw_template)) { |
| 2560 | if (TemplateKWLoc && (ObjectType || SS.isSet())) { |
| 2561 | TemplateSpecified = true; |
| 2562 | *TemplateKWLoc = ConsumeToken(); |
| 2563 | } else { |
| 2564 | SourceLocation TemplateLoc = ConsumeToken(); |
| 2565 | Diag(TemplateLoc, diag::err_unexpected_template_in_unqualified_id) |
| 2566 | << FixItHint::CreateRemoval(TemplateLoc); |
| 2567 | } |
Douglas Gregor | b22ee88 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2568 | } |
| 2569 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2570 | // unqualified-id: |
| 2571 | // identifier |
| 2572 | // template-id (when it hasn't already been annotated) |
| 2573 | if (Tok.is(tok::identifier)) { |
| 2574 | // Consume the identifier. |
| 2575 | IdentifierInfo *Id = Tok.getIdentifierInfo(); |
| 2576 | SourceLocation IdLoc = ConsumeToken(); |
| 2577 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2578 | if (!getLangOpts().CPlusPlus) { |
Douglas Gregor | 411e5ac | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 2579 | // If we're not in C++, only identifiers matter. Record the |
| 2580 | // identifier and return. |
| 2581 | Result.setIdentifier(Id, IdLoc); |
| 2582 | return false; |
| 2583 | } |
| 2584 | |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 2585 | ParsedTemplateTy TemplateName; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2586 | if (AllowConstructorName && |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2587 | Actions.isCurrentClassName(*Id, getCurScope(), &SS)) { |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2588 | // We have parsed a constructor name. |
Richard Smith | 69bc9aa | 2018-06-22 19:50:19 +0000 | [diff] [blame] | 2589 | ParsedType Ty = Actions.getConstructorName(*Id, IdLoc, getCurScope(), SS, |
| 2590 | EnteringContext); |
Richard Smith | 715ee07 | 2018-06-20 21:58:20 +0000 | [diff] [blame] | 2591 | if (!Ty) |
| 2592 | return true; |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2593 | Result.setConstructorName(Ty, IdLoc, IdLoc); |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 2594 | } else if (getLangOpts().CPlusPlus17 && |
Richard Smith | 3584515 | 2017-02-07 01:37:30 +0000 | [diff] [blame] | 2595 | AllowDeductionGuide && SS.isEmpty() && |
| 2596 | Actions.isDeductionGuideName(getCurScope(), *Id, IdLoc, |
| 2597 | &TemplateName)) { |
| 2598 | // We have parsed a template-name naming a deduction guide. |
| 2599 | Result.setDeductionGuideName(TemplateName, IdLoc); |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2600 | } else { |
| 2601 | // We have parsed an identifier. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2602 | Result.setIdentifier(Id, IdLoc); |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2603 | } |
| 2604 | |
| 2605 | // If the next token is a '<', we may have a template. |
Richard Smith | c08b693 | 2018-04-27 02:00:13 +0000 | [diff] [blame] | 2606 | TemplateTy Template; |
| 2607 | if (Tok.is(tok::less)) |
| 2608 | return ParseUnqualifiedIdTemplateId( |
| 2609 | SS, TemplateKWLoc ? *TemplateKWLoc : SourceLocation(), Id, IdLoc, |
| 2610 | EnteringContext, ObjectType, Result, TemplateSpecified); |
| 2611 | else if (TemplateSpecified && |
| 2612 | Actions.ActOnDependentTemplateName( |
| 2613 | getCurScope(), SS, *TemplateKWLoc, Result, ObjectType, |
| 2614 | EnteringContext, Template, |
| 2615 | /*AllowInjectedClassName*/ true) == TNK_Non_template) |
| 2616 | return true; |
| 2617 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2618 | return false; |
| 2619 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2620 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2621 | // unqualified-id: |
| 2622 | // template-id (already parsed and annotated) |
| 2623 | if (Tok.is(tok::annot_template_id)) { |
Argyrios Kyrtzidis | c0c5dd2 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2624 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2625 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2626 | // If the template-name names the current class, then this is a constructor |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2627 | if (AllowConstructorName && TemplateId->Name && |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2628 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) { |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2629 | if (SS.isSet()) { |
| 2630 | // C++ [class.qual]p2 specifies that a qualified template-name |
| 2631 | // is taken as the constructor name where a constructor can be |
| 2632 | // declared. Thus, the template arguments are extraneous, so |
| 2633 | // complain about them and remove them entirely. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2634 | Diag(TemplateId->TemplateNameLoc, |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2635 | diag::err_out_of_line_constructor_template_id) |
| 2636 | << TemplateId->Name |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2637 | << FixItHint::CreateRemoval( |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2638 | SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc)); |
Richard Smith | 715ee07 | 2018-06-20 21:58:20 +0000 | [diff] [blame] | 2639 | ParsedType Ty = Actions.getConstructorName( |
Richard Smith | 69bc9aa | 2018-06-22 19:50:19 +0000 | [diff] [blame] | 2640 | *TemplateId->Name, TemplateId->TemplateNameLoc, getCurScope(), SS, |
| 2641 | EnteringContext); |
Richard Smith | 715ee07 | 2018-06-20 21:58:20 +0000 | [diff] [blame] | 2642 | if (!Ty) |
| 2643 | return true; |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2644 | Result.setConstructorName(Ty, TemplateId->TemplateNameLoc, |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2645 | TemplateId->RAngleLoc); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 2646 | ConsumeAnnotationToken(); |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2647 | return false; |
| 2648 | } |
| 2649 | |
| 2650 | Result.setConstructorTemplateId(TemplateId); |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 2651 | ConsumeAnnotationToken(); |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2652 | return false; |
| 2653 | } |
| 2654 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2655 | // We have already parsed a template-id; consume the annotation token as |
| 2656 | // our unqualified-id. |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2657 | Result.setTemplateId(TemplateId); |
Richard Smith | c08b693 | 2018-04-27 02:00:13 +0000 | [diff] [blame] | 2658 | SourceLocation TemplateLoc = TemplateId->TemplateKWLoc; |
| 2659 | if (TemplateLoc.isValid()) { |
| 2660 | if (TemplateKWLoc && (ObjectType || SS.isSet())) |
| 2661 | *TemplateKWLoc = TemplateLoc; |
| 2662 | else |
| 2663 | Diag(TemplateLoc, diag::err_unexpected_template_in_unqualified_id) |
| 2664 | << FixItHint::CreateRemoval(TemplateLoc); |
| 2665 | } |
Richard Smith | af3b325 | 2017-05-18 19:21:48 +0000 | [diff] [blame] | 2666 | ConsumeAnnotationToken(); |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2667 | return false; |
| 2668 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2669 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2670 | // unqualified-id: |
| 2671 | // operator-function-id |
| 2672 | // conversion-function-id |
| 2673 | if (Tok.is(tok::kw_operator)) { |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2674 | if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result)) |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2675 | return true; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2676 | |
Alexis Hunt | ed0530f | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 2677 | // If we have an operator-function-id or a literal-operator-id and the next |
| 2678 | // token is a '<', we may have a |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2679 | // |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2680 | // template-id: |
| 2681 | // operator-function-id < template-argument-list[opt] > |
Richard Smith | c08b693 | 2018-04-27 02:00:13 +0000 | [diff] [blame] | 2682 | TemplateTy Template; |
Faisal Vali | 2ab8c15 | 2017-12-30 04:15:27 +0000 | [diff] [blame] | 2683 | if ((Result.getKind() == UnqualifiedIdKind::IK_OperatorFunctionId || |
| 2684 | Result.getKind() == UnqualifiedIdKind::IK_LiteralOperatorId) && |
Richard Smith | c08b693 | 2018-04-27 02:00:13 +0000 | [diff] [blame] | 2685 | Tok.is(tok::less)) |
| 2686 | return ParseUnqualifiedIdTemplateId( |
| 2687 | SS, TemplateKWLoc ? *TemplateKWLoc : SourceLocation(), nullptr, |
| 2688 | SourceLocation(), EnteringContext, ObjectType, Result, |
| 2689 | TemplateSpecified); |
| 2690 | else if (TemplateSpecified && |
| 2691 | Actions.ActOnDependentTemplateName( |
| 2692 | getCurScope(), SS, *TemplateKWLoc, Result, ObjectType, |
| 2693 | EnteringContext, Template, |
| 2694 | /*AllowInjectedClassName*/ true) == TNK_Non_template) |
| 2695 | return true; |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 2696 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2697 | return false; |
| 2698 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2699 | |
| 2700 | if (getLangOpts().CPlusPlus && |
Douglas Gregor | 411e5ac | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 2701 | (AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) { |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2702 | // C++ [expr.unary.op]p10: |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2703 | // There is an ambiguity in the unary-expression ~X(), where X is a |
| 2704 | // class-name. The ambiguity is resolved in favor of treating ~ as a |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2705 | // unary complement rather than treating ~X as referring to a destructor. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2706 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2707 | // Parse the '~'. |
| 2708 | SourceLocation TildeLoc = ConsumeToken(); |
David Blaikie | ecd8a94 | 2011-12-08 16:13:53 +0000 | [diff] [blame] | 2709 | |
| 2710 | if (SS.isEmpty() && Tok.is(tok::kw_decltype)) { |
| 2711 | DeclSpec DS(AttrFactory); |
| 2712 | SourceLocation EndLoc = ParseDecltypeSpecifier(DS); |
Richard Smith | ef2cd8f | 2017-02-08 20:39:08 +0000 | [diff] [blame] | 2713 | if (ParsedType Type = |
| 2714 | Actions.getDestructorTypeForDecltype(DS, ObjectType)) { |
David Blaikie | ecd8a94 | 2011-12-08 16:13:53 +0000 | [diff] [blame] | 2715 | Result.setDestructorName(TildeLoc, Type, EndLoc); |
| 2716 | return false; |
| 2717 | } |
| 2718 | return true; |
| 2719 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2720 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2721 | // Parse the class-name. |
| 2722 | if (Tok.isNot(tok::identifier)) { |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 2723 | Diag(Tok, diag::err_destructor_tilde_identifier); |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2724 | return true; |
| 2725 | } |
| 2726 | |
Richard Smith | efa6f73 | 2014-09-06 02:06:12 +0000 | [diff] [blame] | 2727 | // If the user wrote ~T::T, correct it to T::~T. |
Richard Smith | 64e033f | 2015-01-15 00:48:52 +0000 | [diff] [blame] | 2728 | DeclaratorScopeObj DeclScopeObj(*this, SS); |
Nico Weber | 10d02b5 | 2015-02-02 04:18:38 +0000 | [diff] [blame] | 2729 | if (!TemplateSpecified && NextToken().is(tok::coloncolon)) { |
Nico Weber | f9e37be | 2015-01-30 16:53:11 +0000 | [diff] [blame] | 2730 | // Don't let ParseOptionalCXXScopeSpecifier() "correct" |
| 2731 | // `int A; struct { ~A::A(); };` to `int A; struct { ~A:A(); };`, |
| 2732 | // it will confuse this recovery logic. |
| 2733 | ColonProtectionRAIIObject ColonRAII(*this, false); |
| 2734 | |
Richard Smith | efa6f73 | 2014-09-06 02:06:12 +0000 | [diff] [blame] | 2735 | if (SS.isSet()) { |
| 2736 | AnnotateScopeToken(SS, /*NewAnnotation*/true); |
| 2737 | SS.clear(); |
| 2738 | } |
| 2739 | if (ParseOptionalCXXScopeSpecifier(SS, ObjectType, EnteringContext)) |
| 2740 | return true; |
Nico Weber | 7f8ec52 | 2015-02-02 05:33:50 +0000 | [diff] [blame] | 2741 | if (SS.isNotEmpty()) |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 2742 | ObjectType = nullptr; |
Nico Weber | d004586 | 2015-01-30 04:05:15 +0000 | [diff] [blame] | 2743 | if (Tok.isNot(tok::identifier) || NextToken().is(tok::coloncolon) || |
Benjamin Kramer | 3012e59 | 2015-03-29 14:35:39 +0000 | [diff] [blame] | 2744 | !SS.isSet()) { |
Richard Smith | efa6f73 | 2014-09-06 02:06:12 +0000 | [diff] [blame] | 2745 | Diag(TildeLoc, diag::err_destructor_tilde_scope); |
| 2746 | return true; |
| 2747 | } |
| 2748 | |
| 2749 | // Recover as if the tilde had been written before the identifier. |
| 2750 | Diag(TildeLoc, diag::err_destructor_tilde_scope) |
| 2751 | << FixItHint::CreateRemoval(TildeLoc) |
| 2752 | << FixItHint::CreateInsertion(Tok.getLocation(), "~"); |
Richard Smith | 64e033f | 2015-01-15 00:48:52 +0000 | [diff] [blame] | 2753 | |
| 2754 | // Temporarily enter the scope for the rest of this function. |
| 2755 | if (Actions.ShouldEnterDeclaratorScope(getCurScope(), SS)) |
| 2756 | DeclScopeObj.EnterDeclaratorScope(); |
Richard Smith | efa6f73 | 2014-09-06 02:06:12 +0000 | [diff] [blame] | 2757 | } |
| 2758 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2759 | // Parse the class-name (or template-name in a simple-template-id). |
| 2760 | IdentifierInfo *ClassName = Tok.getIdentifierInfo(); |
| 2761 | SourceLocation ClassNameLoc = ConsumeToken(); |
Richard Smith | efa6f73 | 2014-09-06 02:06:12 +0000 | [diff] [blame] | 2762 | |
Richard Smith | c08b693 | 2018-04-27 02:00:13 +0000 | [diff] [blame] | 2763 | if (Tok.is(tok::less)) { |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 2764 | Result.setDestructorName(TildeLoc, nullptr, ClassNameLoc); |
Richard Smith | c08b693 | 2018-04-27 02:00:13 +0000 | [diff] [blame] | 2765 | return ParseUnqualifiedIdTemplateId( |
| 2766 | SS, TemplateKWLoc ? *TemplateKWLoc : SourceLocation(), ClassName, |
| 2767 | ClassNameLoc, EnteringContext, ObjectType, Result, TemplateSpecified); |
Douglas Gregor | 30d60cb | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 2768 | } |
Richard Smith | efa6f73 | 2014-09-06 02:06:12 +0000 | [diff] [blame] | 2769 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2770 | // Note that this is a destructor name. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2771 | ParsedType Ty = Actions.getDestructorName(TildeLoc, *ClassName, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2772 | ClassNameLoc, getCurScope(), |
| 2773 | SS, ObjectType, |
| 2774 | EnteringContext); |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 2775 | if (!Ty) |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2776 | return true; |
Douglas Gregor | fe17d25 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 2777 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2778 | Result.setDestructorName(TildeLoc, Ty, ClassNameLoc); |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2779 | return false; |
| 2780 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2781 | |
Douglas Gregor | 30d60cb | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 2782 | Diag(Tok, diag::err_expected_unqualified_id) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2783 | << getLangOpts().CPlusPlus; |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 2784 | return true; |
| 2785 | } |
| 2786 | |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2787 | /// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate |
| 2788 | /// memory in a typesafe manner and call constructors. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2789 | /// |
Chris Lattner | 109faf2 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 2790 | /// This method is called to parse the new expression after the optional :: has |
| 2791 | /// been already parsed. If the :: was present, "UseGlobal" is true and "Start" |
| 2792 | /// is its location. Otherwise, "Start" is the location of the 'new' token. |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2793 | /// |
| 2794 | /// new-expression: |
| 2795 | /// '::'[opt] 'new' new-placement[opt] new-type-id |
| 2796 | /// new-initializer[opt] |
| 2797 | /// '::'[opt] 'new' new-placement[opt] '(' type-id ')' |
| 2798 | /// new-initializer[opt] |
| 2799 | /// |
| 2800 | /// new-placement: |
| 2801 | /// '(' expression-list ')' |
| 2802 | /// |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2803 | /// new-type-id: |
| 2804 | /// type-specifier-seq new-declarator[opt] |
Douglas Gregor | a3a020a | 2011-04-15 19:40:02 +0000 | [diff] [blame] | 2805 | /// [GNU] attributes type-specifier-seq new-declarator[opt] |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2806 | /// |
| 2807 | /// new-declarator: |
| 2808 | /// ptr-operator new-declarator[opt] |
| 2809 | /// direct-new-declarator |
| 2810 | /// |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2811 | /// new-initializer: |
| 2812 | /// '(' expression-list[opt] ')' |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2813 | /// [C++0x] braced-init-list |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2814 | /// |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2815 | ExprResult |
Chris Lattner | 109faf2 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 2816 | Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) { |
| 2817 | assert(Tok.is(tok::kw_new) && "expected 'new' token"); |
| 2818 | ConsumeToken(); // Consume 'new' |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2819 | |
| 2820 | // A '(' now can be a new-placement or the '(' wrapping the type-id in the |
| 2821 | // second form of new-expression. It can't be a new-type-id. |
| 2822 | |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 2823 | ExprVector PlacementArgs; |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2824 | SourceLocation PlacementLParen, PlacementRParen; |
| 2825 | |
Douglas Gregor | f2753b3 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 2826 | SourceRange TypeIdParens; |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2827 | DeclSpec DS(AttrFactory); |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 2828 | Declarator DeclaratorInfo(DS, DeclaratorContext::CXXNewContext); |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2829 | if (Tok.is(tok::l_paren)) { |
| 2830 | // If it turns out to be a placement, we change the type location. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2831 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 2832 | T.consumeOpen(); |
| 2833 | PlacementLParen = T.getOpenLocation(); |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2834 | if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2835 | SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch); |
Sebastian Redl | d65cea8 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 2836 | return ExprError(); |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2837 | } |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2838 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2839 | T.consumeClose(); |
| 2840 | PlacementRParen = T.getCloseLocation(); |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2841 | if (PlacementRParen.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2842 | SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch); |
Sebastian Redl | d65cea8 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 2843 | return ExprError(); |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2844 | } |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2845 | |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2846 | if (PlacementArgs.empty()) { |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2847 | // Reset the placement locations. There was no placement. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2848 | TypeIdParens = T.getRange(); |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2849 | PlacementLParen = PlacementRParen = SourceLocation(); |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2850 | } else { |
| 2851 | // We still need the type. |
| 2852 | if (Tok.is(tok::l_paren)) { |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2853 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 2854 | T.consumeOpen(); |
Douglas Gregor | a3a020a | 2011-04-15 19:40:02 +0000 | [diff] [blame] | 2855 | MaybeParseGNUAttributes(DeclaratorInfo); |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2856 | ParseSpecifierQualifierList(DS); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2857 | DeclaratorInfo.SetSourceRange(DS.getSourceRange()); |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2858 | ParseDeclarator(DeclaratorInfo); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2859 | T.consumeClose(); |
| 2860 | TypeIdParens = T.getRange(); |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2861 | } else { |
Douglas Gregor | a3a020a | 2011-04-15 19:40:02 +0000 | [diff] [blame] | 2862 | MaybeParseGNUAttributes(DeclaratorInfo); |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2863 | if (ParseCXXTypeSpecifierSeq(DS)) |
| 2864 | DeclaratorInfo.setInvalidType(true); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2865 | else { |
| 2866 | DeclaratorInfo.SetSourceRange(DS.getSourceRange()); |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2867 | ParseDeclaratorInternal(DeclaratorInfo, |
| 2868 | &Parser::ParseDirectNewDeclarator); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2869 | } |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2870 | } |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2871 | } |
| 2872 | } else { |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2873 | // A new-type-id is a simplified type-id, where essentially the |
| 2874 | // direct-declarator is replaced by a direct-new-declarator. |
Douglas Gregor | a3a020a | 2011-04-15 19:40:02 +0000 | [diff] [blame] | 2875 | MaybeParseGNUAttributes(DeclaratorInfo); |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2876 | if (ParseCXXTypeSpecifierSeq(DS)) |
| 2877 | DeclaratorInfo.setInvalidType(true); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2878 | else { |
| 2879 | DeclaratorInfo.SetSourceRange(DS.getSourceRange()); |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2880 | ParseDeclaratorInternal(DeclaratorInfo, |
| 2881 | &Parser::ParseDirectNewDeclarator); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2882 | } |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2883 | } |
Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 2884 | if (DeclaratorInfo.isInvalidType()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2885 | SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch); |
Sebastian Redl | d65cea8 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 2886 | return ExprError(); |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2887 | } |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2888 | |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 2889 | ExprResult Initializer; |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2890 | |
| 2891 | if (Tok.is(tok::l_paren)) { |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 2892 | SourceLocation ConstructorLParen, ConstructorRParen; |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 2893 | ExprVector ConstructorArgs; |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2894 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 2895 | T.consumeOpen(); |
| 2896 | ConstructorLParen = T.getOpenLocation(); |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2897 | if (Tok.isNot(tok::r_paren)) { |
| 2898 | CommaLocsTy CommaLocs; |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 2899 | auto RunSignatureHelp = [&]() { |
| 2900 | ParsedType TypeRep = |
| 2901 | Actions.ActOnTypeName(getCurScope(), DeclaratorInfo).get(); |
| 2902 | QualType PreferredType = Actions.ProduceConstructorSignatureHelp( |
| 2903 | getCurScope(), TypeRep.get()->getCanonicalTypeInternal(), |
| 2904 | DeclaratorInfo.getEndLoc(), ConstructorArgs, ConstructorLParen); |
| 2905 | CalledSignatureHelp = true; |
| 2906 | return PreferredType; |
| 2907 | }; |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 2908 | if (ParseExpressionList(ConstructorArgs, CommaLocs, [&] { |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 2909 | PreferredType.enterFunctionArgument(Tok.getLocation(), |
| 2910 | RunSignatureHelp); |
Kadir Cetinkaya | a32d253 | 2018-09-10 13:46:28 +0000 | [diff] [blame] | 2911 | })) { |
Ilya Biryukov | ff2a997 | 2019-02-26 11:01:50 +0000 | [diff] [blame] | 2912 | if (PP.isCodeCompletionReached() && !CalledSignatureHelp) |
| 2913 | RunSignatureHelp(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2914 | SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch); |
Sebastian Redl | d65cea8 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 2915 | return ExprError(); |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2916 | } |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2917 | } |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2918 | T.consumeClose(); |
| 2919 | ConstructorRParen = T.getCloseLocation(); |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2920 | if (ConstructorRParen.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2921 | SkipUntil(tok::semi, StopAtSemi | StopBeforeMatch); |
Sebastian Redl | d65cea8 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 2922 | return ExprError(); |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 2923 | } |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 2924 | Initializer = Actions.ActOnParenListExpr(ConstructorLParen, |
| 2925 | ConstructorRParen, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2926 | ConstructorArgs); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2927 | } else if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus11) { |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 2928 | Diag(Tok.getLocation(), |
| 2929 | diag::warn_cxx98_compat_generalized_initializer_lists); |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 2930 | Initializer = ParseBraceInitializer(); |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2931 | } |
Sebastian Redl | 6047f07 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 2932 | if (Initializer.isInvalid()) |
| 2933 | return Initializer; |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2934 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 2935 | return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2936 | PlacementArgs, PlacementRParen, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2937 | TypeIdParens, DeclaratorInfo, Initializer.get()); |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2938 | } |
| 2939 | |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2940 | /// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be |
| 2941 | /// passed to ParseDeclaratorInternal. |
| 2942 | /// |
| 2943 | /// direct-new-declarator: |
Richard Smith | b9fb121 | 2019-05-06 03:47:15 +0000 | [diff] [blame] | 2944 | /// '[' expression[opt] ']' |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2945 | /// direct-new-declarator '[' constant-expression ']' |
| 2946 | /// |
Chris Lattner | 109faf2 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 2947 | void Parser::ParseDirectNewDeclarator(Declarator &D) { |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2948 | // Parse the array dimensions. |
Richard Smith | b9fb121 | 2019-05-06 03:47:15 +0000 | [diff] [blame] | 2949 | bool First = true; |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2950 | while (Tok.is(tok::l_square)) { |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 2951 | // An array-size expression can't start with a lambda. |
| 2952 | if (CheckProhibitedCXX11Attribute()) |
| 2953 | continue; |
| 2954 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2955 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 2956 | T.consumeOpen(); |
| 2957 | |
Richard Smith | b9fb121 | 2019-05-06 03:47:15 +0000 | [diff] [blame] | 2958 | ExprResult Size = |
| 2959 | First ? (Tok.is(tok::r_square) ? ExprResult() : ParseExpression()) |
| 2960 | : ParseConstantExpression(); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2961 | if (Size.isInvalid()) { |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2962 | // Recover |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2963 | SkipUntil(tok::r_square, StopAtSemi); |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2964 | return; |
| 2965 | } |
Richard Smith | b9fb121 | 2019-05-06 03:47:15 +0000 | [diff] [blame] | 2966 | First = false; |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2967 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2968 | T.consumeClose(); |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2969 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2970 | // Attributes here appertain to the array type. C++11 [expr.new]p5. |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 2971 | ParsedAttributes Attrs(AttrFactory); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 2972 | MaybeParseCXX11Attributes(Attrs); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 2973 | |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2974 | D.AddTypeInfo(DeclaratorChunk::getArray(0, |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2975 | /*static=*/false, /*star=*/false, |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 2976 | Size.get(), T.getOpenLocation(), |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2977 | T.getCloseLocation()), |
Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 2978 | std::move(Attrs), T.getCloseLocation()); |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2979 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2980 | if (T.getCloseLocation().isInvalid()) |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2981 | return; |
| 2982 | } |
| 2983 | } |
| 2984 | |
| 2985 | /// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id. |
| 2986 | /// This ambiguity appears in the syntax of the C++ new operator. |
| 2987 | /// |
| 2988 | /// new-expression: |
| 2989 | /// '::'[opt] 'new' new-placement[opt] '(' type-id ')' |
| 2990 | /// new-initializer[opt] |
| 2991 | /// |
| 2992 | /// new-placement: |
| 2993 | /// '(' expression-list ')' |
| 2994 | /// |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 2995 | bool Parser::ParseExpressionListOrTypeId( |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2996 | SmallVectorImpl<Expr*> &PlacementArgs, |
Chris Lattner | 109faf2 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 2997 | Declarator &D) { |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2998 | // The '(' was already consumed. |
| 2999 | if (isTypeIdInParens()) { |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 3000 | ParseSpecifierQualifierList(D.getMutableDeclSpec()); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3001 | D.SetSourceRange(D.getDeclSpec().getSourceRange()); |
Sebastian Redl | 351bb78 | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 3002 | ParseDeclarator(D); |
Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 3003 | return D.isInvalidType(); |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3004 | } |
| 3005 | |
| 3006 | // It's not a type, it has to be an expression list. |
| 3007 | // Discard the comma locations - ActOnCXXNew has enough parameters. |
| 3008 | CommaLocsTy CommaLocs; |
| 3009 | return ParseExpressionList(PlacementArgs, CommaLocs); |
| 3010 | } |
| 3011 | |
| 3012 | /// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used |
| 3013 | /// to free memory allocated by new. |
| 3014 | /// |
Chris Lattner | 109faf2 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 3015 | /// This method is called to parse the 'delete' expression after the optional |
| 3016 | /// '::' has been already parsed. If the '::' was present, "UseGlobal" is true |
| 3017 | /// and "Start" is its location. Otherwise, "Start" is the location of the |
| 3018 | /// 'delete' token. |
| 3019 | /// |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3020 | /// delete-expression: |
| 3021 | /// '::'[opt] 'delete' cast-expression |
| 3022 | /// '::'[opt] 'delete' '[' ']' cast-expression |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3023 | ExprResult |
Chris Lattner | 109faf2 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 3024 | Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) { |
| 3025 | assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword"); |
| 3026 | ConsumeToken(); // Consume 'delete' |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3027 | |
| 3028 | // Array delete? |
| 3029 | bool ArrayDelete = false; |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3030 | if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) { |
Richard Smith | 10c6072 | 2012-08-09 19:01:51 +0000 | [diff] [blame] | 3031 | // C++11 [expr.delete]p1: |
| 3032 | // Whenever the delete keyword is followed by empty square brackets, it |
| 3033 | // shall be interpreted as [array delete]. |
| 3034 | // [Footnote: A lambda expression with a lambda-introducer that consists |
| 3035 | // of empty square brackets can follow the delete keyword if |
| 3036 | // the lambda expression is enclosed in parentheses.] |
| 3037 | // FIXME: Produce a better diagnostic if the '[]' is unambiguously a |
| 3038 | // lambda-introducer. |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3039 | ArrayDelete = true; |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3040 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 3041 | |
| 3042 | T.consumeOpen(); |
| 3043 | T.consumeClose(); |
| 3044 | if (T.getCloseLocation().isInvalid()) |
Sebastian Redl | d65cea8 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 3045 | return ExprError(); |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3046 | } |
| 3047 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3048 | ExprResult Operand(ParseCastExpression(false)); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 3049 | if (Operand.isInvalid()) |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 3050 | return Operand; |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3051 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3052 | return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, Operand.get()); |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3053 | } |
Sebastian Redl | baad4e7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 3054 | |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 3055 | static TypeTrait TypeTraitFromTokKind(tok::TokenKind kind) { |
| 3056 | switch (kind) { |
| 3057 | default: llvm_unreachable("Not a known type trait"); |
Alp Toker | 95e7ff2 | 2014-01-01 05:57:51 +0000 | [diff] [blame] | 3058 | #define TYPE_TRAIT_1(Spelling, Name, Key) \ |
| 3059 | case tok::kw_ ## Spelling: return UTT_ ## Name; |
Alp Toker | cbb9034 | 2013-12-13 20:49:58 +0000 | [diff] [blame] | 3060 | #define TYPE_TRAIT_2(Spelling, Name, Key) \ |
| 3061 | case tok::kw_ ## Spelling: return BTT_ ## Name; |
| 3062 | #include "clang/Basic/TokenKinds.def" |
Alp Toker | 40f9b1c | 2013-12-12 21:23:03 +0000 | [diff] [blame] | 3063 | #define TYPE_TRAIT_N(Spelling, Name, Key) \ |
| 3064 | case tok::kw_ ## Spelling: return TT_ ## Name; |
| 3065 | #include "clang/Basic/TokenKinds.def" |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 3066 | } |
| 3067 | } |
| 3068 | |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3069 | static ArrayTypeTrait ArrayTypeTraitFromTokKind(tok::TokenKind kind) { |
| 3070 | switch(kind) { |
| 3071 | default: llvm_unreachable("Not a known binary type trait"); |
| 3072 | case tok::kw___array_rank: return ATT_ArrayRank; |
| 3073 | case tok::kw___array_extent: return ATT_ArrayExtent; |
| 3074 | } |
| 3075 | } |
| 3076 | |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3077 | static ExpressionTrait ExpressionTraitFromTokKind(tok::TokenKind kind) { |
| 3078 | switch(kind) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3079 | default: llvm_unreachable("Not a known unary expression trait."); |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3080 | case tok::kw___is_lvalue_expr: return ET_IsLValueExpr; |
| 3081 | case tok::kw___is_rvalue_expr: return ET_IsRValueExpr; |
| 3082 | } |
| 3083 | } |
| 3084 | |
Alp Toker | 40f9b1c | 2013-12-12 21:23:03 +0000 | [diff] [blame] | 3085 | static unsigned TypeTraitArity(tok::TokenKind kind) { |
| 3086 | switch (kind) { |
| 3087 | default: llvm_unreachable("Not a known type trait"); |
| 3088 | #define TYPE_TRAIT(N,Spelling,K) case tok::kw_##Spelling: return N; |
| 3089 | #include "clang/Basic/TokenKinds.def" |
Francois Pichet | 9dfa3ce | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3090 | } |
Francois Pichet | 9dfa3ce | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3091 | } |
| 3092 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3093 | /// Parse the built-in type-trait pseudo-functions that allow |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 3094 | /// implementation of the TR1/C++11 type traits templates. |
| 3095 | /// |
| 3096 | /// primary-expression: |
Alp Toker | 40f9b1c | 2013-12-12 21:23:03 +0000 | [diff] [blame] | 3097 | /// unary-type-trait '(' type-id ')' |
| 3098 | /// binary-type-trait '(' type-id ',' type-id ')' |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 3099 | /// type-trait '(' type-id-seq ')' |
| 3100 | /// |
| 3101 | /// type-id-seq: |
| 3102 | /// type-id ...[opt] type-id-seq[opt] |
| 3103 | /// |
| 3104 | ExprResult Parser::ParseTypeTrait() { |
Alp Toker | 40f9b1c | 2013-12-12 21:23:03 +0000 | [diff] [blame] | 3105 | tok::TokenKind Kind = Tok.getKind(); |
| 3106 | unsigned Arity = TypeTraitArity(Kind); |
| 3107 | |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 3108 | SourceLocation Loc = ConsumeToken(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3109 | |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 3110 | BalancedDelimiterTracker Parens(*this, tok::l_paren); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 3111 | if (Parens.expectAndConsume()) |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 3112 | return ExprError(); |
| 3113 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3114 | SmallVector<ParsedType, 2> Args; |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 3115 | do { |
| 3116 | // Parse the next type. |
| 3117 | TypeResult Ty = ParseTypeName(); |
| 3118 | if (Ty.isInvalid()) { |
| 3119 | Parens.skipToEnd(); |
| 3120 | return ExprError(); |
| 3121 | } |
| 3122 | |
| 3123 | // Parse the ellipsis, if present. |
| 3124 | if (Tok.is(tok::ellipsis)) { |
| 3125 | Ty = Actions.ActOnPackExpansion(Ty.get(), ConsumeToken()); |
| 3126 | if (Ty.isInvalid()) { |
| 3127 | Parens.skipToEnd(); |
| 3128 | return ExprError(); |
| 3129 | } |
| 3130 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3131 | |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 3132 | // Add this type to the list of arguments. |
| 3133 | Args.push_back(Ty.get()); |
Alp Toker | a3ebe6e | 2013-12-17 14:12:37 +0000 | [diff] [blame] | 3134 | } while (TryConsumeToken(tok::comma)); |
| 3135 | |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 3136 | if (Parens.consumeClose()) |
| 3137 | return ExprError(); |
Alp Toker | 40f9b1c | 2013-12-12 21:23:03 +0000 | [diff] [blame] | 3138 | |
| 3139 | SourceLocation EndLoc = Parens.getCloseLocation(); |
| 3140 | |
| 3141 | if (Arity && Args.size() != Arity) { |
| 3142 | Diag(EndLoc, diag::err_type_trait_arity) |
| 3143 | << Arity << 0 << (Arity > 1) << (int)Args.size() << SourceRange(Loc); |
| 3144 | return ExprError(); |
| 3145 | } |
| 3146 | |
| 3147 | if (!Arity && Args.empty()) { |
| 3148 | Diag(EndLoc, diag::err_type_trait_arity) |
| 3149 | << 1 << 1 << 1 << (int)Args.size() << SourceRange(Loc); |
| 3150 | return ExprError(); |
| 3151 | } |
| 3152 | |
Alp Toker | 88f64e6 | 2013-12-13 21:19:30 +0000 | [diff] [blame] | 3153 | return Actions.ActOnTypeTrait(TypeTraitFromTokKind(Kind), Loc, Args, EndLoc); |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 3154 | } |
| 3155 | |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3156 | /// ParseArrayTypeTrait - Parse the built-in array type-trait |
| 3157 | /// pseudo-functions. |
| 3158 | /// |
| 3159 | /// primary-expression: |
| 3160 | /// [Embarcadero] '__array_rank' '(' type-id ')' |
| 3161 | /// [Embarcadero] '__array_extent' '(' type-id ',' expression ')' |
| 3162 | /// |
| 3163 | ExprResult Parser::ParseArrayTypeTrait() { |
| 3164 | ArrayTypeTrait ATT = ArrayTypeTraitFromTokKind(Tok.getKind()); |
| 3165 | SourceLocation Loc = ConsumeToken(); |
| 3166 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3167 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 3168 | if (T.expectAndConsume()) |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3169 | return ExprError(); |
| 3170 | |
| 3171 | TypeResult Ty = ParseTypeName(); |
| 3172 | if (Ty.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3173 | SkipUntil(tok::comma, StopAtSemi); |
| 3174 | SkipUntil(tok::r_paren, StopAtSemi); |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3175 | return ExprError(); |
| 3176 | } |
| 3177 | |
| 3178 | switch (ATT) { |
| 3179 | case ATT_ArrayRank: { |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3180 | T.consumeClose(); |
Craig Topper | 161e4db | 2014-05-21 06:02:52 +0000 | [diff] [blame] | 3181 | return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), nullptr, |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3182 | T.getCloseLocation()); |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3183 | } |
| 3184 | case ATT_ArrayExtent: { |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 3185 | if (ExpectAndConsume(tok::comma)) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3186 | SkipUntil(tok::r_paren, StopAtSemi); |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3187 | return ExprError(); |
| 3188 | } |
| 3189 | |
| 3190 | ExprResult DimExpr = ParseExpression(); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3191 | T.consumeClose(); |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3192 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3193 | return Actions.ActOnArrayTypeTrait(ATT, Loc, Ty.get(), DimExpr.get(), |
| 3194 | T.getCloseLocation()); |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3195 | } |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3196 | } |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 3197 | llvm_unreachable("Invalid ArrayTypeTrait!"); |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3198 | } |
| 3199 | |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3200 | /// ParseExpressionTrait - Parse built-in expression-trait |
| 3201 | /// pseudo-functions like __is_lvalue_expr( xxx ). |
| 3202 | /// |
| 3203 | /// primary-expression: |
| 3204 | /// [Embarcadero] expression-trait '(' expression ')' |
| 3205 | /// |
| 3206 | ExprResult Parser::ParseExpressionTrait() { |
| 3207 | ExpressionTrait ET = ExpressionTraitFromTokKind(Tok.getKind()); |
| 3208 | SourceLocation Loc = ConsumeToken(); |
| 3209 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3210 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Alp Toker | 383d2c4 | 2014-01-01 03:08:43 +0000 | [diff] [blame] | 3211 | if (T.expectAndConsume()) |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3212 | return ExprError(); |
| 3213 | |
| 3214 | ExprResult Expr = ParseExpression(); |
| 3215 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3216 | T.consumeClose(); |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3217 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3218 | return Actions.ActOnExpressionTrait(ET, Loc, Expr.get(), |
| 3219 | T.getCloseLocation()); |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3220 | } |
| 3221 | |
| 3222 | |
Argyrios Kyrtzidis | 12179bc | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 3223 | /// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a |
| 3224 | /// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate |
| 3225 | /// based on the context past the parens. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3226 | ExprResult |
Argyrios Kyrtzidis | 12179bc | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 3227 | Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3228 | ParsedType &CastTy, |
Richard Smith | 87e11a4 | 2014-05-15 02:43:47 +0000 | [diff] [blame] | 3229 | BalancedDelimiterTracker &Tracker, |
| 3230 | ColonProtectionRAIIObject &ColonProt) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3231 | assert(getLangOpts().CPlusPlus && "Should only be called for C++!"); |
Argyrios Kyrtzidis | 12179bc | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 3232 | assert(ExprType == CastExpr && "Compound literals are not ambiguous!"); |
| 3233 | assert(isTypeIdInParens() && "Not a type-id!"); |
| 3234 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3235 | ExprResult Result(true); |
David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 3236 | CastTy = nullptr; |
Argyrios Kyrtzidis | 12179bc | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 3237 | |
| 3238 | // We need to disambiguate a very ugly part of the C++ syntax: |
| 3239 | // |
| 3240 | // (T())x; - type-id |
| 3241 | // (T())*x; - type-id |
| 3242 | // (T())/x; - expression |
| 3243 | // (T()); - expression |
| 3244 | // |
| 3245 | // The bad news is that we cannot use the specialized tentative parser, since |
| 3246 | // it can only verify that the thing inside the parens can be parsed as |
| 3247 | // type-id, it is not useful for determining the context past the parens. |
| 3248 | // |
| 3249 | // The good news is that the parser can disambiguate this part without |
Argyrios Kyrtzidis | 24ad692 | 2009-05-22 15:12:46 +0000 | [diff] [blame] | 3250 | // making any unnecessary Action calls. |
Argyrios Kyrtzidis | f73f2d2 | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 3251 | // |
| 3252 | // It uses a scheme similar to parsing inline methods. The parenthesized |
| 3253 | // tokens are cached, the context that follows is determined (possibly by |
| 3254 | // parsing a cast-expression), and then we re-introduce the cached tokens |
| 3255 | // into the token stream and parse them appropriately. |
Argyrios Kyrtzidis | 12179bc | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 3256 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3257 | ParenParseOption ParseAs; |
Argyrios Kyrtzidis | f73f2d2 | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 3258 | CachedTokens Toks; |
Argyrios Kyrtzidis | 12179bc | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 3259 | |
Argyrios Kyrtzidis | f73f2d2 | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 3260 | // Store the tokens of the parentheses. We will parse them after we determine |
| 3261 | // the context that follows them. |
Argyrios Kyrtzidis | 8d7bdba | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 3262 | if (!ConsumeAndStoreUntil(tok::r_paren, Toks)) { |
Argyrios Kyrtzidis | f73f2d2 | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 3263 | // We didn't find the ')' we expected. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3264 | Tracker.consumeClose(); |
Argyrios Kyrtzidis | 12179bc | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 3265 | return ExprError(); |
| 3266 | } |
| 3267 | |
Argyrios Kyrtzidis | 12179bc | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 3268 | if (Tok.is(tok::l_brace)) { |
Argyrios Kyrtzidis | f73f2d2 | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 3269 | ParseAs = CompoundLiteral; |
| 3270 | } else { |
| 3271 | bool NotCastExpr; |
Eli Friedman | cf7530f | 2009-05-25 19:41:42 +0000 | [diff] [blame] | 3272 | if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) { |
| 3273 | NotCastExpr = true; |
| 3274 | } else { |
| 3275 | // Try parsing the cast-expression that may follow. |
| 3276 | // If it is not a cast-expression, NotCastExpr will be true and no token |
| 3277 | // will be consumed. |
Richard Smith | 87e11a4 | 2014-05-15 02:43:47 +0000 | [diff] [blame] | 3278 | ColonProt.restore(); |
Eli Friedman | cf7530f | 2009-05-25 19:41:42 +0000 | [diff] [blame] | 3279 | Result = ParseCastExpression(false/*isUnaryExpression*/, |
| 3280 | false/*isAddressofOperand*/, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3281 | NotCastExpr, |
Argyrios Kyrtzidis | 7192a3b | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 3282 | // type-id has priority. |
Kaelyn Uhrain | 77e21fc | 2012-01-25 20:49:08 +0000 | [diff] [blame] | 3283 | IsTypeCast); |
Eli Friedman | cf7530f | 2009-05-25 19:41:42 +0000 | [diff] [blame] | 3284 | } |
Argyrios Kyrtzidis | f73f2d2 | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 3285 | |
| 3286 | // If we parsed a cast-expression, it's really a type-id, otherwise it's |
| 3287 | // an expression. |
| 3288 | ParseAs = NotCastExpr ? SimpleExpr : CastExpr; |
Argyrios Kyrtzidis | 12179bc | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 3289 | } |
| 3290 | |
Alexey Bataev | 703a93c | 2016-02-04 04:22:09 +0000 | [diff] [blame] | 3291 | // Create a fake EOF to mark end of Toks buffer. |
| 3292 | Token AttrEnd; |
| 3293 | AttrEnd.startToken(); |
| 3294 | AttrEnd.setKind(tok::eof); |
| 3295 | AttrEnd.setLocation(Tok.getLocation()); |
| 3296 | AttrEnd.setEofData(Toks.data()); |
| 3297 | Toks.push_back(AttrEnd); |
| 3298 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3299 | // The current token should go after the cached tokens. |
Argyrios Kyrtzidis | f73f2d2 | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 3300 | Toks.push_back(Tok); |
| 3301 | // Re-enter the stored parenthesized tokens into the token stream, so we may |
| 3302 | // parse them now. |
Ilya Biryukov | 929af67 | 2019-05-17 09:32:05 +0000 | [diff] [blame^] | 3303 | PP.EnterTokenStream(Toks, /*DisableMacroExpansion*/ true, |
| 3304 | /*IsReinject*/ true); |
Argyrios Kyrtzidis | f73f2d2 | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 3305 | // Drop the current token and bring the first cached one. It's the same token |
| 3306 | // as when we entered this function. |
| 3307 | ConsumeAnyToken(); |
Argyrios Kyrtzidis | 12179bc | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 3308 | |
Argyrios Kyrtzidis | f73f2d2 | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 3309 | if (ParseAs >= CompoundLiteral) { |
Argyrios Kyrtzidis | 7192a3b | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 3310 | // Parse the type declarator. |
| 3311 | DeclSpec DS(AttrFactory); |
Faisal Vali | 421b2d1 | 2017-12-29 05:41:00 +0000 | [diff] [blame] | 3312 | Declarator DeclaratorInfo(DS, DeclaratorContext::TypeNameContext); |
Richard Smith | 87e11a4 | 2014-05-15 02:43:47 +0000 | [diff] [blame] | 3313 | { |
| 3314 | ColonProtectionRAIIObject InnerColonProtection(*this); |
| 3315 | ParseSpecifierQualifierList(DS); |
| 3316 | ParseDeclarator(DeclaratorInfo); |
| 3317 | } |
Argyrios Kyrtzidis | 12179bc | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 3318 | |
Argyrios Kyrtzidis | f73f2d2 | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 3319 | // Match the ')'. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3320 | Tracker.consumeClose(); |
Richard Smith | 87e11a4 | 2014-05-15 02:43:47 +0000 | [diff] [blame] | 3321 | ColonProt.restore(); |
Argyrios Kyrtzidis | 12179bc | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 3322 | |
Alexey Bataev | 703a93c | 2016-02-04 04:22:09 +0000 | [diff] [blame] | 3323 | // Consume EOF marker for Toks buffer. |
| 3324 | assert(Tok.is(tok::eof) && Tok.getEofData() == AttrEnd.getEofData()); |
| 3325 | ConsumeAnyToken(); |
| 3326 | |
Argyrios Kyrtzidis | f73f2d2 | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 3327 | if (ParseAs == CompoundLiteral) { |
| 3328 | ExprType = CompoundLiteral; |
Richard Smith | aba8b36 | 2014-05-15 02:51:15 +0000 | [diff] [blame] | 3329 | if (DeclaratorInfo.isInvalidType()) |
| 3330 | return ExprError(); |
| 3331 | |
| 3332 | TypeResult Ty = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); |
Richard Smith | 87e11a4 | 2014-05-15 02:43:47 +0000 | [diff] [blame] | 3333 | return ParseCompoundLiteralExpression(Ty.get(), |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3334 | Tracker.getOpenLocation(), |
| 3335 | Tracker.getCloseLocation()); |
Argyrios Kyrtzidis | f73f2d2 | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 3336 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3337 | |
Argyrios Kyrtzidis | f73f2d2 | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 3338 | // We parsed '(' type-id ')' and the thing after it wasn't a '{'. |
| 3339 | assert(ParseAs == CastExpr); |
| 3340 | |
Argyrios Kyrtzidis | 7192a3b | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 3341 | if (DeclaratorInfo.isInvalidType()) |
Argyrios Kyrtzidis | f73f2d2 | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 3342 | return ExprError(); |
| 3343 | |
Argyrios Kyrtzidis | f73f2d2 | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 3344 | // Result is what ParseCastExpression returned earlier. |
Argyrios Kyrtzidis | 12179bc | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 3345 | if (!Result.isInvalid()) |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3346 | Result = Actions.ActOnCastExpr(getCurScope(), Tracker.getOpenLocation(), |
| 3347 | DeclaratorInfo, CastTy, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3348 | Tracker.getCloseLocation(), Result.get()); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 3349 | return Result; |
Argyrios Kyrtzidis | 12179bc | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 3350 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3351 | |
Argyrios Kyrtzidis | f73f2d2 | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 3352 | // Not a compound literal, and not followed by a cast-expression. |
| 3353 | assert(ParseAs == SimpleExpr); |
Argyrios Kyrtzidis | 12179bc | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 3354 | |
Argyrios Kyrtzidis | 12179bc | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 3355 | ExprType = SimpleExpr; |
Argyrios Kyrtzidis | f73f2d2 | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 3356 | Result = ParseExpression(); |
Argyrios Kyrtzidis | 12179bc | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 3357 | if (!Result.isInvalid() && Tok.is(tok::r_paren)) |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3358 | Result = Actions.ActOnParenExpr(Tracker.getOpenLocation(), |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3359 | Tok.getLocation(), Result.get()); |
Argyrios Kyrtzidis | 12179bc | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 3360 | |
| 3361 | // Match the ')'. |
| 3362 | if (Result.isInvalid()) { |
Alexey Bataev | 703a93c | 2016-02-04 04:22:09 +0000 | [diff] [blame] | 3363 | while (Tok.isNot(tok::eof)) |
| 3364 | ConsumeAnyToken(); |
| 3365 | assert(Tok.getEofData() == AttrEnd.getEofData()); |
| 3366 | ConsumeAnyToken(); |
Argyrios Kyrtzidis | 12179bc | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 3367 | return ExprError(); |
| 3368 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3369 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3370 | Tracker.consumeClose(); |
Alexey Bataev | 703a93c | 2016-02-04 04:22:09 +0000 | [diff] [blame] | 3371 | // Consume EOF marker for Toks buffer. |
| 3372 | assert(Tok.is(tok::eof) && Tok.getEofData() == AttrEnd.getEofData()); |
| 3373 | ConsumeAnyToken(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 3374 | return Result; |
Argyrios Kyrtzidis | 12179bc | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 3375 | } |