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