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