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