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