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