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