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