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