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