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