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