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