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