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