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