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