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