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