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