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