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