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