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