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