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