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