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