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