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