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