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