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