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