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