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