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