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