Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- ParseExprCXX.cpp - C++ Expression Parsing ------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Expression parsing implementation for C++. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 14 | #include "clang/Parse/ParseDiagnostic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 15 | #include "clang/Parse/Parser.h" |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 16 | #include "clang/Parse/DeclSpec.h" |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 17 | #include "clang/Parse/Template.h" |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 18 | #include "llvm/Support/ErrorHandling.h" |
| 19 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 22 | /// \brief Parse global scope or nested-name-specifier if present. |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 23 | /// |
| 24 | /// Parses a C++ global scope specifier ('::') or nested-name-specifier (which |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 25 | /// may be preceded by '::'). Note that this routine will not parse ::new or |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 26 | /// ::delete; it will just leave them in the token stream. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 27 | /// |
| 28 | /// '::'[opt] nested-name-specifier |
| 29 | /// '::' |
| 30 | /// |
| 31 | /// nested-name-specifier: |
| 32 | /// type-name '::' |
| 33 | /// namespace-name '::' |
| 34 | /// nested-name-specifier identifier '::' |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 35 | /// nested-name-specifier 'template'[opt] simple-template-id '::' |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 36 | /// |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 37 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 38 | /// \param SS the scope specifier that will be set to the parsed |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 39 | /// nested-name-specifier (or empty) |
| 40 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 41 | /// \param ObjectType if this nested-name-specifier is being parsed following |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 42 | /// the "." or "->" of a member access expression, this parameter provides the |
| 43 | /// type of the object whose members are being accessed. |
| 44 | /// |
| 45 | /// \param EnteringContext whether we will be entering into the context of |
| 46 | /// the nested-name-specifier after parsing it. |
| 47 | /// |
| 48 | /// \returns true if a scope specifier was parsed. |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 49 | bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS, |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 50 | Action::TypeTy *ObjectType, |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 51 | bool EnteringContext) { |
Argyrios Kyrtzidis | 4bdd91c | 2008-11-26 21:41:52 +0000 | [diff] [blame] | 52 | assert(getLang().CPlusPlus && |
Chris Lattner | 7452c6f | 2009-01-05 01:24:05 +0000 | [diff] [blame] | 53 | "Call sites of this function should be guarded by checking for C++"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 54 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 55 | if (Tok.is(tok::annot_cxxscope)) { |
Douglas Gregor | 3507369 | 2009-03-26 23:56:24 +0000 | [diff] [blame] | 56 | SS.setScopeRep(Tok.getAnnotationValue()); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 57 | SS.setRange(Tok.getAnnotationRange()); |
| 58 | ConsumeToken(); |
Argyrios Kyrtzidis | 4bdd91c | 2008-11-26 21:41:52 +0000 | [diff] [blame] | 59 | return true; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 60 | } |
Chris Lattner | e607e80 | 2009-01-04 21:14:15 +0000 | [diff] [blame] | 61 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 62 | bool HasScopeSpecifier = false; |
| 63 | |
Chris Lattner | 5b45473 | 2009-01-05 03:55:46 +0000 | [diff] [blame] | 64 | if (Tok.is(tok::coloncolon)) { |
| 65 | // ::new and ::delete aren't nested-name-specifiers. |
| 66 | tok::TokenKind NextKind = NextToken().getKind(); |
| 67 | if (NextKind == tok::kw_new || NextKind == tok::kw_delete) |
| 68 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 69 | |
Chris Lattner | 55a7cef | 2009-01-05 00:13:00 +0000 | [diff] [blame] | 70 | // '::' - Global scope qualifier. |
Chris Lattner | 357089d | 2009-01-05 02:07:19 +0000 | [diff] [blame] | 71 | SourceLocation CCLoc = ConsumeToken(); |
Chris Lattner | 357089d | 2009-01-05 02:07:19 +0000 | [diff] [blame] | 72 | SS.setBeginLoc(CCLoc); |
Douglas Gregor | 3507369 | 2009-03-26 23:56:24 +0000 | [diff] [blame] | 73 | SS.setScopeRep(Actions.ActOnCXXGlobalScopeSpecifier(CurScope, CCLoc)); |
Chris Lattner | 357089d | 2009-01-05 02:07:19 +0000 | [diff] [blame] | 74 | SS.setEndLoc(CCLoc); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 75 | HasScopeSpecifier = true; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 78 | while (true) { |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 79 | if (HasScopeSpecifier) { |
| 80 | // C++ [basic.lookup.classref]p5: |
| 81 | // If the qualified-id has the form |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 82 | // |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 83 | // ::class-name-or-namespace-name::... |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 84 | // |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 85 | // the class-name-or-namespace-name is looked up in global scope as a |
| 86 | // class-name or namespace-name. |
| 87 | // |
| 88 | // To implement this, we clear out the object type as soon as we've |
| 89 | // seen a leading '::' or part of a nested-name-specifier. |
| 90 | ObjectType = 0; |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 91 | |
| 92 | if (Tok.is(tok::code_completion)) { |
| 93 | // Code completion for a nested-name-specifier, where the code |
| 94 | // code completion token follows the '::'. |
| 95 | Actions.CodeCompleteQualifiedId(CurScope, SS, EnteringContext); |
| 96 | ConsumeToken(); |
| 97 | } |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 98 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 99 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 100 | // nested-name-specifier: |
Chris Lattner | 77cf72a | 2009-06-26 03:47:46 +0000 | [diff] [blame] | 101 | // nested-name-specifier 'template'[opt] simple-template-id '::' |
| 102 | |
| 103 | // Parse the optional 'template' keyword, then make sure we have |
| 104 | // 'identifier <' after it. |
| 105 | if (Tok.is(tok::kw_template)) { |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 106 | // If we don't have a scope specifier or an object type, this isn't a |
Eli Friedman | eab975d | 2009-08-29 04:08:08 +0000 | [diff] [blame] | 107 | // nested-name-specifier, since they aren't allowed to start with |
| 108 | // 'template'. |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 109 | if (!HasScopeSpecifier && !ObjectType) |
Eli Friedman | eab975d | 2009-08-29 04:08:08 +0000 | [diff] [blame] | 110 | break; |
| 111 | |
Douglas Gregor | 7bb87fc | 2009-11-11 16:39:34 +0000 | [diff] [blame] | 112 | TentativeParsingAction TPA(*this); |
Chris Lattner | 77cf72a | 2009-06-26 03:47:46 +0000 | [diff] [blame] | 113 | SourceLocation TemplateKWLoc = ConsumeToken(); |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 114 | |
| 115 | UnqualifiedId TemplateName; |
| 116 | if (Tok.is(tok::identifier)) { |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 117 | // Consume the identifier. |
Douglas Gregor | 7bb87fc | 2009-11-11 16:39:34 +0000 | [diff] [blame] | 118 | TemplateName.setIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 119 | ConsumeToken(); |
| 120 | } else if (Tok.is(tok::kw_operator)) { |
| 121 | if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, |
Douglas Gregor | 7bb87fc | 2009-11-11 16:39:34 +0000 | [diff] [blame] | 122 | TemplateName)) { |
| 123 | TPA.Commit(); |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 124 | break; |
Douglas Gregor | 7bb87fc | 2009-11-11 16:39:34 +0000 | [diff] [blame] | 125 | } |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 126 | |
| 127 | if (TemplateName.getKind() != UnqualifiedId::IK_OperatorFunctionId) { |
| 128 | Diag(TemplateName.getSourceRange().getBegin(), |
| 129 | diag::err_id_after_template_in_nested_name_spec) |
| 130 | << TemplateName.getSourceRange(); |
Douglas Gregor | 7bb87fc | 2009-11-11 16:39:34 +0000 | [diff] [blame] | 131 | TPA.Commit(); |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 132 | break; |
| 133 | } |
| 134 | } else { |
Douglas Gregor | 7bb87fc | 2009-11-11 16:39:34 +0000 | [diff] [blame] | 135 | TPA.Revert(); |
Chris Lattner | 77cf72a | 2009-06-26 03:47:46 +0000 | [diff] [blame] | 136 | break; |
| 137 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 138 | |
Douglas Gregor | 7bb87fc | 2009-11-11 16:39:34 +0000 | [diff] [blame] | 139 | // If the next token is not '<', we have a qualified-id that refers |
| 140 | // to a template name, such as T::template apply, but is not a |
| 141 | // template-id. |
| 142 | if (Tok.isNot(tok::less)) { |
| 143 | TPA.Revert(); |
| 144 | break; |
| 145 | } |
| 146 | |
| 147 | // Commit to parsing the template-id. |
| 148 | TPA.Commit(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 149 | TemplateTy Template |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 150 | = Actions.ActOnDependentTemplateName(TemplateKWLoc, SS, TemplateName, |
Douglas Gregor | a481edb | 2009-11-20 23:39:24 +0000 | [diff] [blame^] | 151 | ObjectType, EnteringContext); |
Eli Friedman | eab975d | 2009-08-29 04:08:08 +0000 | [diff] [blame] | 152 | if (!Template) |
| 153 | break; |
Chris Lattner | c8e27cc | 2009-06-26 04:27:47 +0000 | [diff] [blame] | 154 | if (AnnotateTemplateIdToken(Template, TNK_Dependent_template_name, |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 155 | &SS, TemplateName, TemplateKWLoc, false)) |
Chris Lattner | c8e27cc | 2009-06-26 04:27:47 +0000 | [diff] [blame] | 156 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 157 | |
Chris Lattner | 77cf72a | 2009-06-26 03:47:46 +0000 | [diff] [blame] | 158 | continue; |
| 159 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 160 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 161 | if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 162 | // We have |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 163 | // |
| 164 | // simple-template-id '::' |
| 165 | // |
| 166 | // So we need to check whether the simple-template-id is of the |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 167 | // right kind (it should name a type or be dependent), and then |
| 168 | // convert it into a type within the nested-name-specifier. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 169 | TemplateIdAnnotation *TemplateId |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 170 | = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); |
| 171 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 172 | if (TemplateId->Kind == TNK_Type_template || |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 173 | TemplateId->Kind == TNK_Dependent_template_name) { |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 174 | AnnotateTemplateIdTokenAsType(&SS); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 175 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 176 | assert(Tok.is(tok::annot_typename) && |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 177 | "AnnotateTemplateIdTokenAsType isn't working"); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 178 | Token TypeToken = Tok; |
| 179 | ConsumeToken(); |
| 180 | assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!"); |
| 181 | SourceLocation CCLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 182 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 183 | if (!HasScopeSpecifier) { |
| 184 | SS.setBeginLoc(TypeToken.getLocation()); |
| 185 | HasScopeSpecifier = true; |
| 186 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 187 | |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 188 | if (TypeToken.getAnnotationValue()) |
| 189 | SS.setScopeRep( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 190 | Actions.ActOnCXXNestedNameSpecifier(CurScope, SS, |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 191 | TypeToken.getAnnotationValue(), |
| 192 | TypeToken.getAnnotationRange(), |
| 193 | CCLoc)); |
| 194 | else |
| 195 | SS.setScopeRep(0); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 196 | SS.setEndLoc(CCLoc); |
| 197 | continue; |
Chris Lattner | 67b9e83 | 2009-06-26 03:45:46 +0000 | [diff] [blame] | 198 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 199 | |
Chris Lattner | 67b9e83 | 2009-06-26 03:45:46 +0000 | [diff] [blame] | 200 | assert(false && "FIXME: Only type template names supported here"); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Chris Lattner | 5c7f786 | 2009-06-26 03:52:38 +0000 | [diff] [blame] | 203 | |
| 204 | // The rest of the nested-name-specifier possibilities start with |
| 205 | // tok::identifier. |
| 206 | if (Tok.isNot(tok::identifier)) |
| 207 | break; |
| 208 | |
| 209 | IdentifierInfo &II = *Tok.getIdentifierInfo(); |
| 210 | |
| 211 | // nested-name-specifier: |
| 212 | // type-name '::' |
| 213 | // namespace-name '::' |
| 214 | // nested-name-specifier identifier '::' |
| 215 | Token Next = NextToken(); |
| 216 | if (Next.is(tok::coloncolon)) { |
| 217 | // We have an identifier followed by a '::'. Lookup this name |
| 218 | // as the name in a nested-name-specifier. |
| 219 | SourceLocation IdLoc = ConsumeToken(); |
| 220 | assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!"); |
| 221 | SourceLocation CCLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 222 | |
Chris Lattner | 5c7f786 | 2009-06-26 03:52:38 +0000 | [diff] [blame] | 223 | if (!HasScopeSpecifier) { |
| 224 | SS.setBeginLoc(IdLoc); |
| 225 | HasScopeSpecifier = true; |
| 226 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 227 | |
Chris Lattner | 5c7f786 | 2009-06-26 03:52:38 +0000 | [diff] [blame] | 228 | if (SS.isInvalid()) |
| 229 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 230 | |
Chris Lattner | 5c7f786 | 2009-06-26 03:52:38 +0000 | [diff] [blame] | 231 | SS.setScopeRep( |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 232 | Actions.ActOnCXXNestedNameSpecifier(CurScope, SS, IdLoc, CCLoc, II, |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 233 | ObjectType, EnteringContext)); |
Chris Lattner | 5c7f786 | 2009-06-26 03:52:38 +0000 | [diff] [blame] | 234 | SS.setEndLoc(CCLoc); |
| 235 | continue; |
| 236 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 237 | |
Chris Lattner | 5c7f786 | 2009-06-26 03:52:38 +0000 | [diff] [blame] | 238 | // nested-name-specifier: |
| 239 | // type-name '<' |
| 240 | if (Next.is(tok::less)) { |
| 241 | TemplateTy Template; |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 242 | UnqualifiedId TemplateName; |
| 243 | TemplateName.setIdentifier(&II, Tok.getLocation()); |
| 244 | if (TemplateNameKind TNK = Actions.isTemplateName(CurScope, SS, |
| 245 | TemplateName, |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 246 | ObjectType, |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 247 | EnteringContext, |
| 248 | Template)) { |
Chris Lattner | 5c7f786 | 2009-06-26 03:52:38 +0000 | [diff] [blame] | 249 | // We have found a template name, so annotate this this token |
| 250 | // with a template-id annotation. We do not permit the |
| 251 | // template-id to be translated into a type annotation, |
| 252 | // because some clients (e.g., the parsing of class template |
| 253 | // specializations) still want to see the original template-id |
| 254 | // token. |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 255 | ConsumeToken(); |
| 256 | if (AnnotateTemplateIdToken(Template, TNK, &SS, TemplateName, |
| 257 | SourceLocation(), false)) |
Chris Lattner | c8e27cc | 2009-06-26 04:27:47 +0000 | [diff] [blame] | 258 | break; |
Chris Lattner | 5c7f786 | 2009-06-26 03:52:38 +0000 | [diff] [blame] | 259 | continue; |
| 260 | } |
| 261 | } |
| 262 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 263 | // We don't have any tokens that form the beginning of a |
| 264 | // nested-name-specifier, so we're done. |
| 265 | break; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 266 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 267 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 268 | return HasScopeSpecifier; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /// ParseCXXIdExpression - Handle id-expression. |
| 272 | /// |
| 273 | /// id-expression: |
| 274 | /// unqualified-id |
| 275 | /// qualified-id |
| 276 | /// |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 277 | /// qualified-id: |
| 278 | /// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id |
| 279 | /// '::' identifier |
| 280 | /// '::' operator-function-id |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 281 | /// '::' template-id |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 282 | /// |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 283 | /// NOTE: The standard specifies that, for qualified-id, the parser does not |
| 284 | /// expect: |
| 285 | /// |
| 286 | /// '::' conversion-function-id |
| 287 | /// '::' '~' class-name |
| 288 | /// |
| 289 | /// This may cause a slight inconsistency on diagnostics: |
| 290 | /// |
| 291 | /// class C {}; |
| 292 | /// namespace A {} |
| 293 | /// void f() { |
| 294 | /// :: A :: ~ C(); // Some Sema error about using destructor with a |
| 295 | /// // namespace. |
| 296 | /// :: ~ C(); // Some Parser error like 'unexpected ~'. |
| 297 | /// } |
| 298 | /// |
| 299 | /// We simplify the parser a bit and make it work like: |
| 300 | /// |
| 301 | /// qualified-id: |
| 302 | /// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id |
| 303 | /// '::' unqualified-id |
| 304 | /// |
| 305 | /// That way Sema can handle and report similar errors for namespaces and the |
| 306 | /// global scope. |
| 307 | /// |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 308 | /// The isAddressOfOperand parameter indicates that this id-expression is a |
| 309 | /// direct operand of the address-of operator. This is, besides member contexts, |
| 310 | /// the only place where a qualified-id naming a non-static class member may |
| 311 | /// appear. |
| 312 | /// |
| 313 | Parser::OwningExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) { |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 314 | // qualified-id: |
| 315 | // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id |
| 316 | // '::' unqualified-id |
| 317 | // |
| 318 | CXXScopeSpec SS; |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 319 | ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false); |
Douglas Gregor | 02a24ee | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 320 | |
| 321 | UnqualifiedId Name; |
| 322 | if (ParseUnqualifiedId(SS, |
| 323 | /*EnteringContext=*/false, |
| 324 | /*AllowDestructorName=*/false, |
| 325 | /*AllowConstructorName=*/false, |
Douglas Gregor | 2d1c214 | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 326 | /*ObjectType=*/0, |
Douglas Gregor | 02a24ee | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 327 | Name)) |
| 328 | return ExprError(); |
| 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, |
Douglas Gregor | a481edb | 2009-11-20 23:39:24 +0000 | [diff] [blame^] | 777 | TemplateName, ObjectType, |
| 778 | EnteringContext); |
Douglas Gregor | 2d1c214 | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 779 | TNK = TNK_Dependent_template_name; |
| 780 | if (!Template.get()) |
| 781 | return true; |
| 782 | } else { |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 783 | TNK = Actions.isTemplateName(CurScope, SS, TemplateName, ObjectType, |
Douglas Gregor | 2d1c214 | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 784 | EnteringContext, Template); |
| 785 | |
| 786 | if (TNK == TNK_Non_template && Id.DestructorName == 0) { |
| 787 | // The identifier following the destructor did not refer to a template |
| 788 | // or to a type. Complain. |
| 789 | if (ObjectType) |
| 790 | Diag(NameLoc, diag::err_ident_in_pseudo_dtor_not_a_type) |
| 791 | << Name; |
| 792 | else |
| 793 | Diag(NameLoc, diag::err_destructor_class_name); |
| 794 | return true; |
| 795 | } |
| 796 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 797 | break; |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 798 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 799 | |
| 800 | default: |
| 801 | return false; |
| 802 | } |
| 803 | |
| 804 | if (TNK == TNK_Non_template) |
| 805 | return false; |
| 806 | |
| 807 | // Parse the enclosed template argument list. |
| 808 | SourceLocation LAngleLoc, RAngleLoc; |
| 809 | TemplateArgList TemplateArgs; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 810 | if (ParseTemplateIdAfterTemplateName(Template, Id.StartLocation, |
| 811 | &SS, true, LAngleLoc, |
| 812 | TemplateArgs, |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 813 | RAngleLoc)) |
| 814 | return true; |
| 815 | |
| 816 | if (Id.getKind() == UnqualifiedId::IK_Identifier || |
| 817 | Id.getKind() == UnqualifiedId::IK_OperatorFunctionId) { |
| 818 | // Form a parsed representation of the template-id to be stored in the |
| 819 | // UnqualifiedId. |
| 820 | TemplateIdAnnotation *TemplateId |
| 821 | = TemplateIdAnnotation::Allocate(TemplateArgs.size()); |
| 822 | |
| 823 | if (Id.getKind() == UnqualifiedId::IK_Identifier) { |
| 824 | TemplateId->Name = Id.Identifier; |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 825 | TemplateId->Operator = OO_None; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 826 | TemplateId->TemplateNameLoc = Id.StartLocation; |
| 827 | } else { |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 828 | TemplateId->Name = 0; |
| 829 | TemplateId->Operator = Id.OperatorFunctionId.Operator; |
| 830 | TemplateId->TemplateNameLoc = Id.StartLocation; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | TemplateId->Template = Template.getAs<void*>(); |
| 834 | TemplateId->Kind = TNK; |
| 835 | TemplateId->LAngleLoc = LAngleLoc; |
| 836 | TemplateId->RAngleLoc = RAngleLoc; |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 837 | ParsedTemplateArgument *Args = TemplateId->getTemplateArgs(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 838 | for (unsigned Arg = 0, ArgEnd = TemplateArgs.size(); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 839 | Arg != ArgEnd; ++Arg) |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 840 | Args[Arg] = TemplateArgs[Arg]; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 841 | |
| 842 | Id.setTemplateId(TemplateId); |
| 843 | return false; |
| 844 | } |
| 845 | |
| 846 | // Bundle the template arguments together. |
| 847 | ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(), |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 848 | TemplateArgs.size()); |
| 849 | |
| 850 | // Constructor and destructor names. |
| 851 | Action::TypeResult Type |
| 852 | = Actions.ActOnTemplateIdType(Template, NameLoc, |
| 853 | LAngleLoc, TemplateArgsPtr, |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 854 | RAngleLoc); |
| 855 | if (Type.isInvalid()) |
| 856 | return true; |
| 857 | |
| 858 | if (Id.getKind() == UnqualifiedId::IK_ConstructorName) |
| 859 | Id.setConstructorName(Type.get(), NameLoc, RAngleLoc); |
| 860 | else |
| 861 | Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc); |
| 862 | |
| 863 | return false; |
| 864 | } |
| 865 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 866 | /// \brief Parse an operator-function-id or conversion-function-id as part |
| 867 | /// of a C++ unqualified-id. |
| 868 | /// |
| 869 | /// This routine is responsible only for parsing the operator-function-id or |
| 870 | /// conversion-function-id; it does not handle template arguments in any way. |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 871 | /// |
| 872 | /// \code |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 873 | /// operator-function-id: [C++ 13.5] |
| 874 | /// 'operator' operator |
| 875 | /// |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 876 | /// operator: one of |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 877 | /// new delete new[] delete[] |
| 878 | /// + - * / % ^ & | ~ |
| 879 | /// ! = < > += -= *= /= %= |
| 880 | /// ^= &= |= << >> >>= <<= == != |
| 881 | /// <= >= && || ++ -- , ->* -> |
| 882 | /// () [] |
| 883 | /// |
| 884 | /// conversion-function-id: [C++ 12.3.2] |
| 885 | /// operator conversion-type-id |
| 886 | /// |
| 887 | /// conversion-type-id: |
| 888 | /// type-specifier-seq conversion-declarator[opt] |
| 889 | /// |
| 890 | /// conversion-declarator: |
| 891 | /// ptr-operator conversion-declarator[opt] |
| 892 | /// \endcode |
| 893 | /// |
| 894 | /// \param The nested-name-specifier that preceded this unqualified-id. If |
| 895 | /// non-empty, then we are parsing the unqualified-id of a qualified-id. |
| 896 | /// |
| 897 | /// \param EnteringContext whether we are entering the scope of the |
| 898 | /// nested-name-specifier. |
| 899 | /// |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 900 | /// \param ObjectType if this unqualified-id occurs within a member access |
| 901 | /// expression, the type of the base object whose member is being accessed. |
| 902 | /// |
| 903 | /// \param Result on a successful parse, contains the parsed unqualified-id. |
| 904 | /// |
| 905 | /// \returns true if parsing fails, false otherwise. |
| 906 | bool Parser::ParseUnqualifiedIdOperator(CXXScopeSpec &SS, bool EnteringContext, |
| 907 | TypeTy *ObjectType, |
| 908 | UnqualifiedId &Result) { |
| 909 | assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword"); |
| 910 | |
| 911 | // Consume the 'operator' keyword. |
| 912 | SourceLocation KeywordLoc = ConsumeToken(); |
| 913 | |
| 914 | // Determine what kind of operator name we have. |
| 915 | unsigned SymbolIdx = 0; |
| 916 | SourceLocation SymbolLocations[3]; |
| 917 | OverloadedOperatorKind Op = OO_None; |
| 918 | switch (Tok.getKind()) { |
| 919 | case tok::kw_new: |
| 920 | case tok::kw_delete: { |
| 921 | bool isNew = Tok.getKind() == tok::kw_new; |
| 922 | // Consume the 'new' or 'delete'. |
| 923 | SymbolLocations[SymbolIdx++] = ConsumeToken(); |
| 924 | if (Tok.is(tok::l_square)) { |
| 925 | // Consume the '['. |
| 926 | SourceLocation LBracketLoc = ConsumeBracket(); |
| 927 | // Consume the ']'. |
| 928 | SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square, |
| 929 | LBracketLoc); |
| 930 | if (RBracketLoc.isInvalid()) |
| 931 | return true; |
| 932 | |
| 933 | SymbolLocations[SymbolIdx++] = LBracketLoc; |
| 934 | SymbolLocations[SymbolIdx++] = RBracketLoc; |
| 935 | Op = isNew? OO_Array_New : OO_Array_Delete; |
| 936 | } else { |
| 937 | Op = isNew? OO_New : OO_Delete; |
| 938 | } |
| 939 | break; |
| 940 | } |
| 941 | |
| 942 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 943 | case tok::Token: \ |
| 944 | SymbolLocations[SymbolIdx++] = ConsumeToken(); \ |
| 945 | Op = OO_##Name; \ |
| 946 | break; |
| 947 | #define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly) |
| 948 | #include "clang/Basic/OperatorKinds.def" |
| 949 | |
| 950 | case tok::l_paren: { |
| 951 | // Consume the '('. |
| 952 | SourceLocation LParenLoc = ConsumeParen(); |
| 953 | // Consume the ')'. |
| 954 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, |
| 955 | LParenLoc); |
| 956 | if (RParenLoc.isInvalid()) |
| 957 | return true; |
| 958 | |
| 959 | SymbolLocations[SymbolIdx++] = LParenLoc; |
| 960 | SymbolLocations[SymbolIdx++] = RParenLoc; |
| 961 | Op = OO_Call; |
| 962 | break; |
| 963 | } |
| 964 | |
| 965 | case tok::l_square: { |
| 966 | // Consume the '['. |
| 967 | SourceLocation LBracketLoc = ConsumeBracket(); |
| 968 | // Consume the ']'. |
| 969 | SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square, |
| 970 | LBracketLoc); |
| 971 | if (RBracketLoc.isInvalid()) |
| 972 | return true; |
| 973 | |
| 974 | SymbolLocations[SymbolIdx++] = LBracketLoc; |
| 975 | SymbolLocations[SymbolIdx++] = RBracketLoc; |
| 976 | Op = OO_Subscript; |
| 977 | break; |
| 978 | } |
| 979 | |
| 980 | case tok::code_completion: { |
| 981 | // Code completion for the operator name. |
| 982 | Actions.CodeCompleteOperatorName(CurScope); |
| 983 | |
| 984 | // Consume the operator token. |
| 985 | ConsumeToken(); |
| 986 | |
| 987 | // Don't try to parse any further. |
| 988 | return true; |
| 989 | } |
| 990 | |
| 991 | default: |
| 992 | break; |
| 993 | } |
| 994 | |
| 995 | if (Op != OO_None) { |
| 996 | // We have parsed an operator-function-id. |
| 997 | Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations); |
| 998 | return false; |
| 999 | } |
| 1000 | |
| 1001 | // Parse a conversion-function-id. |
| 1002 | // |
| 1003 | // conversion-function-id: [C++ 12.3.2] |
| 1004 | // operator conversion-type-id |
| 1005 | // |
| 1006 | // conversion-type-id: |
| 1007 | // type-specifier-seq conversion-declarator[opt] |
| 1008 | // |
| 1009 | // conversion-declarator: |
| 1010 | // ptr-operator conversion-declarator[opt] |
| 1011 | |
| 1012 | // Parse the type-specifier-seq. |
| 1013 | DeclSpec DS; |
Douglas Gregor | f6e6fc8 | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 1014 | if (ParseCXXTypeSpecifierSeq(DS)) // FIXME: ObjectType? |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 1015 | return true; |
| 1016 | |
| 1017 | // Parse the conversion-declarator, which is merely a sequence of |
| 1018 | // ptr-operators. |
| 1019 | Declarator D(DS, Declarator::TypeNameContext); |
| 1020 | ParseDeclaratorInternal(D, /*DirectDeclParser=*/0); |
| 1021 | |
| 1022 | // Finish up the type. |
| 1023 | Action::TypeResult Ty = Actions.ActOnTypeName(CurScope, D); |
| 1024 | if (Ty.isInvalid()) |
| 1025 | return true; |
| 1026 | |
| 1027 | // Note that this is a conversion-function-id. |
| 1028 | Result.setConversionFunctionId(KeywordLoc, Ty.get(), |
| 1029 | D.getSourceRange().getEnd()); |
| 1030 | return false; |
| 1031 | } |
| 1032 | |
| 1033 | /// \brief Parse a C++ unqualified-id (or a C identifier), which describes the |
| 1034 | /// name of an entity. |
| 1035 | /// |
| 1036 | /// \code |
| 1037 | /// unqualified-id: [C++ expr.prim.general] |
| 1038 | /// identifier |
| 1039 | /// operator-function-id |
| 1040 | /// conversion-function-id |
| 1041 | /// [C++0x] literal-operator-id [TODO] |
| 1042 | /// ~ class-name |
| 1043 | /// template-id |
| 1044 | /// |
| 1045 | /// \endcode |
| 1046 | /// |
| 1047 | /// \param The nested-name-specifier that preceded this unqualified-id. If |
| 1048 | /// non-empty, then we are parsing the unqualified-id of a qualified-id. |
| 1049 | /// |
| 1050 | /// \param EnteringContext whether we are entering the scope of the |
| 1051 | /// nested-name-specifier. |
| 1052 | /// |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1053 | /// \param AllowDestructorName whether we allow parsing of a destructor name. |
| 1054 | /// |
| 1055 | /// \param AllowConstructorName whether we allow parsing a constructor name. |
| 1056 | /// |
Douglas Gregor | 46df8cc | 2009-11-03 21:24:04 +0000 | [diff] [blame] | 1057 | /// \param ObjectType if this unqualified-id occurs within a member access |
| 1058 | /// expression, the type of the base object whose member is being accessed. |
| 1059 | /// |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1060 | /// \param Result on a successful parse, contains the parsed unqualified-id. |
| 1061 | /// |
| 1062 | /// \returns true if parsing fails, false otherwise. |
| 1063 | bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext, |
| 1064 | bool AllowDestructorName, |
| 1065 | bool AllowConstructorName, |
Douglas Gregor | 2d1c214 | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 1066 | TypeTy *ObjectType, |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1067 | UnqualifiedId &Result) { |
| 1068 | // unqualified-id: |
| 1069 | // identifier |
| 1070 | // template-id (when it hasn't already been annotated) |
| 1071 | if (Tok.is(tok::identifier)) { |
| 1072 | // Consume the identifier. |
| 1073 | IdentifierInfo *Id = Tok.getIdentifierInfo(); |
| 1074 | SourceLocation IdLoc = ConsumeToken(); |
| 1075 | |
| 1076 | if (AllowConstructorName && |
| 1077 | Actions.isCurrentClassName(*Id, CurScope, &SS)) { |
| 1078 | // We have parsed a constructor name. |
| 1079 | Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, CurScope, |
| 1080 | &SS, false), |
| 1081 | IdLoc, IdLoc); |
| 1082 | } else { |
| 1083 | // We have parsed an identifier. |
| 1084 | Result.setIdentifier(Id, IdLoc); |
| 1085 | } |
| 1086 | |
| 1087 | // If the next token is a '<', we may have a template. |
| 1088 | if (Tok.is(tok::less)) |
| 1089 | return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext, |
Douglas Gregor | 2d1c214 | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 1090 | ObjectType, Result); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1091 | |
| 1092 | return false; |
| 1093 | } |
| 1094 | |
| 1095 | // unqualified-id: |
| 1096 | // template-id (already parsed and annotated) |
| 1097 | if (Tok.is(tok::annot_template_id)) { |
| 1098 | // FIXME: Could this be a constructor name??? |
| 1099 | |
| 1100 | // We have already parsed a template-id; consume the annotation token as |
| 1101 | // our unqualified-id. |
| 1102 | Result.setTemplateId( |
| 1103 | static_cast<TemplateIdAnnotation*>(Tok.getAnnotationValue())); |
| 1104 | ConsumeToken(); |
| 1105 | return false; |
| 1106 | } |
| 1107 | |
| 1108 | // unqualified-id: |
| 1109 | // operator-function-id |
| 1110 | // conversion-function-id |
| 1111 | if (Tok.is(tok::kw_operator)) { |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 1112 | if (ParseUnqualifiedIdOperator(SS, EnteringContext, ObjectType, Result)) |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1113 | return true; |
| 1114 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 1115 | // If we have an operator-function-id and the next token is a '<', we may |
| 1116 | // have a |
| 1117 | // |
| 1118 | // template-id: |
| 1119 | // operator-function-id < template-argument-list[opt] > |
| 1120 | if (Result.getKind() == UnqualifiedId::IK_OperatorFunctionId && |
| 1121 | Tok.is(tok::less)) |
| 1122 | return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(), |
| 1123 | EnteringContext, ObjectType, |
| 1124 | Result); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1125 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1126 | return false; |
| 1127 | } |
| 1128 | |
| 1129 | if ((AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) { |
| 1130 | // C++ [expr.unary.op]p10: |
| 1131 | // There is an ambiguity in the unary-expression ~X(), where X is a |
| 1132 | // class-name. The ambiguity is resolved in favor of treating ~ as a |
| 1133 | // unary complement rather than treating ~X as referring to a destructor. |
| 1134 | |
| 1135 | // Parse the '~'. |
| 1136 | SourceLocation TildeLoc = ConsumeToken(); |
| 1137 | |
| 1138 | // Parse the class-name. |
| 1139 | if (Tok.isNot(tok::identifier)) { |
| 1140 | Diag(Tok, diag::err_destructor_class_name); |
| 1141 | return true; |
| 1142 | } |
| 1143 | |
| 1144 | // Parse the class-name (or template-name in a simple-template-id). |
| 1145 | IdentifierInfo *ClassName = Tok.getIdentifierInfo(); |
| 1146 | SourceLocation ClassNameLoc = ConsumeToken(); |
| 1147 | |
Douglas Gregor | 2d1c214 | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 1148 | if (Tok.is(tok::less)) { |
| 1149 | Result.setDestructorName(TildeLoc, 0, ClassNameLoc); |
| 1150 | return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc, |
| 1151 | EnteringContext, ObjectType, Result); |
| 1152 | } |
| 1153 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1154 | // Note that this is a destructor name. |
| 1155 | Action::TypeTy *Ty = Actions.getTypeName(*ClassName, ClassNameLoc, |
Douglas Gregor | f6e6fc8 | 2009-11-20 22:03:38 +0000 | [diff] [blame] | 1156 | CurScope, &SS, false, ObjectType); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1157 | if (!Ty) { |
Douglas Gregor | 2d1c214 | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 1158 | if (ObjectType) |
| 1159 | Diag(ClassNameLoc, diag::err_ident_in_pseudo_dtor_not_a_type) |
| 1160 | << ClassName; |
| 1161 | else |
| 1162 | Diag(ClassNameLoc, diag::err_destructor_class_name); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1163 | return true; |
| 1164 | } |
| 1165 | |
| 1166 | Result.setDestructorName(TildeLoc, Ty, ClassNameLoc); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1167 | return false; |
| 1168 | } |
| 1169 | |
Douglas Gregor | 2d1c214 | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 1170 | Diag(Tok, diag::err_expected_unqualified_id) |
| 1171 | << getLang().CPlusPlus; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1172 | return true; |
| 1173 | } |
| 1174 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1175 | /// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate |
| 1176 | /// memory in a typesafe manner and call constructors. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1177 | /// |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 1178 | /// This method is called to parse the new expression after the optional :: has |
| 1179 | /// been already parsed. If the :: was present, "UseGlobal" is true and "Start" |
| 1180 | /// is its location. Otherwise, "Start" is the location of the 'new' token. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1181 | /// |
| 1182 | /// new-expression: |
| 1183 | /// '::'[opt] 'new' new-placement[opt] new-type-id |
| 1184 | /// new-initializer[opt] |
| 1185 | /// '::'[opt] 'new' new-placement[opt] '(' type-id ')' |
| 1186 | /// new-initializer[opt] |
| 1187 | /// |
| 1188 | /// new-placement: |
| 1189 | /// '(' expression-list ')' |
| 1190 | /// |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1191 | /// new-type-id: |
| 1192 | /// type-specifier-seq new-declarator[opt] |
| 1193 | /// |
| 1194 | /// new-declarator: |
| 1195 | /// ptr-operator new-declarator[opt] |
| 1196 | /// direct-new-declarator |
| 1197 | /// |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1198 | /// new-initializer: |
| 1199 | /// '(' expression-list[opt] ')' |
| 1200 | /// [C++0x] braced-init-list [TODO] |
| 1201 | /// |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 1202 | Parser::OwningExprResult |
| 1203 | Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) { |
| 1204 | assert(Tok.is(tok::kw_new) && "expected 'new' token"); |
| 1205 | ConsumeToken(); // Consume 'new' |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1206 | |
| 1207 | // A '(' now can be a new-placement or the '(' wrapping the type-id in the |
| 1208 | // second form of new-expression. It can't be a new-type-id. |
| 1209 | |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 1210 | ExprVector PlacementArgs(Actions); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1211 | SourceLocation PlacementLParen, PlacementRParen; |
| 1212 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1213 | bool ParenTypeId; |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1214 | DeclSpec DS; |
| 1215 | Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1216 | if (Tok.is(tok::l_paren)) { |
| 1217 | // If it turns out to be a placement, we change the type location. |
| 1218 | PlacementLParen = ConsumeParen(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1219 | if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) { |
| 1220 | SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 1221 | return ExprError(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1222 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1223 | |
| 1224 | PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1225 | if (PlacementRParen.isInvalid()) { |
| 1226 | SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 1227 | return ExprError(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1228 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1229 | |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1230 | if (PlacementArgs.empty()) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1231 | // Reset the placement locations. There was no placement. |
| 1232 | PlacementLParen = PlacementRParen = SourceLocation(); |
| 1233 | ParenTypeId = true; |
| 1234 | } else { |
| 1235 | // We still need the type. |
| 1236 | if (Tok.is(tok::l_paren)) { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1237 | SourceLocation LParen = ConsumeParen(); |
| 1238 | ParseSpecifierQualifierList(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1239 | DeclaratorInfo.SetSourceRange(DS.getSourceRange()); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1240 | ParseDeclarator(DeclaratorInfo); |
| 1241 | MatchRHSPunctuation(tok::r_paren, LParen); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1242 | ParenTypeId = true; |
| 1243 | } else { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1244 | if (ParseCXXTypeSpecifierSeq(DS)) |
| 1245 | DeclaratorInfo.setInvalidType(true); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1246 | else { |
| 1247 | DeclaratorInfo.SetSourceRange(DS.getSourceRange()); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1248 | ParseDeclaratorInternal(DeclaratorInfo, |
| 1249 | &Parser::ParseDirectNewDeclarator); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1250 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1251 | ParenTypeId = false; |
| 1252 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1253 | } |
| 1254 | } else { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1255 | // A new-type-id is a simplified type-id, where essentially the |
| 1256 | // direct-declarator is replaced by a direct-new-declarator. |
| 1257 | if (ParseCXXTypeSpecifierSeq(DS)) |
| 1258 | DeclaratorInfo.setInvalidType(true); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1259 | else { |
| 1260 | DeclaratorInfo.SetSourceRange(DS.getSourceRange()); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1261 | ParseDeclaratorInternal(DeclaratorInfo, |
| 1262 | &Parser::ParseDirectNewDeclarator); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1263 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1264 | ParenTypeId = false; |
| 1265 | } |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1266 | if (DeclaratorInfo.isInvalidType()) { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1267 | SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 1268 | return ExprError(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1269 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1270 | |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 1271 | ExprVector ConstructorArgs(Actions); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1272 | SourceLocation ConstructorLParen, ConstructorRParen; |
| 1273 | |
| 1274 | if (Tok.is(tok::l_paren)) { |
| 1275 | ConstructorLParen = ConsumeParen(); |
| 1276 | if (Tok.isNot(tok::r_paren)) { |
| 1277 | CommaLocsTy CommaLocs; |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1278 | if (ParseExpressionList(ConstructorArgs, CommaLocs)) { |
| 1279 | SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 1280 | return ExprError(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1281 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1282 | } |
| 1283 | ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1284 | if (ConstructorRParen.isInvalid()) { |
| 1285 | SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 1286 | return ExprError(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1287 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1288 | } |
| 1289 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1290 | return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen, |
| 1291 | move_arg(PlacementArgs), PlacementRParen, |
| 1292 | ParenTypeId, DeclaratorInfo, ConstructorLParen, |
| 1293 | move_arg(ConstructorArgs), ConstructorRParen); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1296 | /// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be |
| 1297 | /// passed to ParseDeclaratorInternal. |
| 1298 | /// |
| 1299 | /// direct-new-declarator: |
| 1300 | /// '[' expression ']' |
| 1301 | /// direct-new-declarator '[' constant-expression ']' |
| 1302 | /// |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 1303 | void Parser::ParseDirectNewDeclarator(Declarator &D) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1304 | // Parse the array dimensions. |
| 1305 | bool first = true; |
| 1306 | while (Tok.is(tok::l_square)) { |
| 1307 | SourceLocation LLoc = ConsumeBracket(); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 1308 | OwningExprResult Size(first ? ParseExpression() |
| 1309 | : ParseConstantExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1310 | if (Size.isInvalid()) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1311 | // Recover |
| 1312 | SkipUntil(tok::r_square); |
| 1313 | return; |
| 1314 | } |
| 1315 | first = false; |
| 1316 | |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1317 | SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1318 | D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1319 | Size.release(), LLoc, RLoc), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1320 | RLoc); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1321 | |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1322 | if (RLoc.isInvalid()) |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1323 | return; |
| 1324 | } |
| 1325 | } |
| 1326 | |
| 1327 | /// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id. |
| 1328 | /// This ambiguity appears in the syntax of the C++ new operator. |
| 1329 | /// |
| 1330 | /// new-expression: |
| 1331 | /// '::'[opt] 'new' new-placement[opt] '(' type-id ')' |
| 1332 | /// new-initializer[opt] |
| 1333 | /// |
| 1334 | /// new-placement: |
| 1335 | /// '(' expression-list ')' |
| 1336 | /// |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1337 | bool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs, |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 1338 | Declarator &D) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1339 | // The '(' was already consumed. |
| 1340 | if (isTypeIdInParens()) { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1341 | ParseSpecifierQualifierList(D.getMutableDeclSpec()); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1342 | D.SetSourceRange(D.getDeclSpec().getSourceRange()); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1343 | ParseDeclarator(D); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1344 | return D.isInvalidType(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | // It's not a type, it has to be an expression list. |
| 1348 | // Discard the comma locations - ActOnCXXNew has enough parameters. |
| 1349 | CommaLocsTy CommaLocs; |
| 1350 | return ParseExpressionList(PlacementArgs, CommaLocs); |
| 1351 | } |
| 1352 | |
| 1353 | /// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used |
| 1354 | /// to free memory allocated by new. |
| 1355 | /// |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 1356 | /// This method is called to parse the 'delete' expression after the optional |
| 1357 | /// '::' has been already parsed. If the '::' was present, "UseGlobal" is true |
| 1358 | /// and "Start" is its location. Otherwise, "Start" is the location of the |
| 1359 | /// 'delete' token. |
| 1360 | /// |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1361 | /// delete-expression: |
| 1362 | /// '::'[opt] 'delete' cast-expression |
| 1363 | /// '::'[opt] 'delete' '[' ']' cast-expression |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 1364 | Parser::OwningExprResult |
| 1365 | Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) { |
| 1366 | assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword"); |
| 1367 | ConsumeToken(); // Consume 'delete' |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1368 | |
| 1369 | // Array delete? |
| 1370 | bool ArrayDelete = false; |
| 1371 | if (Tok.is(tok::l_square)) { |
| 1372 | ArrayDelete = true; |
| 1373 | SourceLocation LHS = ConsumeBracket(); |
| 1374 | SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS); |
| 1375 | if (RHS.isInvalid()) |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 1376 | return ExprError(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 1379 | OwningExprResult Operand(ParseCastExpression(false)); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1380 | if (Operand.isInvalid()) |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 1381 | return move(Operand); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1382 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1383 | return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, move(Operand)); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1384 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1385 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1386 | static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) { |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1387 | switch(kind) { |
| 1388 | default: assert(false && "Not a known unary type trait."); |
| 1389 | case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign; |
| 1390 | case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy; |
| 1391 | case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor; |
| 1392 | case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign; |
| 1393 | case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy; |
| 1394 | case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor; |
| 1395 | case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor; |
| 1396 | case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor; |
| 1397 | case tok::kw___is_abstract: return UTT_IsAbstract; |
| 1398 | case tok::kw___is_class: return UTT_IsClass; |
| 1399 | case tok::kw___is_empty: return UTT_IsEmpty; |
| 1400 | case tok::kw___is_enum: return UTT_IsEnum; |
| 1401 | case tok::kw___is_pod: return UTT_IsPOD; |
| 1402 | case tok::kw___is_polymorphic: return UTT_IsPolymorphic; |
| 1403 | case tok::kw___is_union: return UTT_IsUnion; |
| 1404 | } |
| 1405 | } |
| 1406 | |
| 1407 | /// ParseUnaryTypeTrait - Parse the built-in unary type-trait |
| 1408 | /// pseudo-functions that allow implementation of the TR1/C++0x type traits |
| 1409 | /// templates. |
| 1410 | /// |
| 1411 | /// primary-expression: |
| 1412 | /// [GNU] unary-type-trait '(' type-id ')' |
| 1413 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1414 | Parser::OwningExprResult Parser::ParseUnaryTypeTrait() { |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1415 | UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind()); |
| 1416 | SourceLocation Loc = ConsumeToken(); |
| 1417 | |
| 1418 | SourceLocation LParen = Tok.getLocation(); |
| 1419 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen)) |
| 1420 | return ExprError(); |
| 1421 | |
| 1422 | // FIXME: Error reporting absolutely sucks! If the this fails to parse a type |
| 1423 | // there will be cryptic errors about mismatched parentheses and missing |
| 1424 | // specifiers. |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 1425 | TypeResult Ty = ParseTypeName(); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1426 | |
| 1427 | SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen); |
| 1428 | |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 1429 | if (Ty.isInvalid()) |
| 1430 | return ExprError(); |
| 1431 | |
| 1432 | return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty.get(), RParen); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1433 | } |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1434 | |
| 1435 | /// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a |
| 1436 | /// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate |
| 1437 | /// based on the context past the parens. |
| 1438 | Parser::OwningExprResult |
| 1439 | Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType, |
| 1440 | TypeTy *&CastTy, |
| 1441 | SourceLocation LParenLoc, |
| 1442 | SourceLocation &RParenLoc) { |
| 1443 | assert(getLang().CPlusPlus && "Should only be called for C++!"); |
| 1444 | assert(ExprType == CastExpr && "Compound literals are not ambiguous!"); |
| 1445 | assert(isTypeIdInParens() && "Not a type-id!"); |
| 1446 | |
| 1447 | OwningExprResult Result(Actions, true); |
| 1448 | CastTy = 0; |
| 1449 | |
| 1450 | // We need to disambiguate a very ugly part of the C++ syntax: |
| 1451 | // |
| 1452 | // (T())x; - type-id |
| 1453 | // (T())*x; - type-id |
| 1454 | // (T())/x; - expression |
| 1455 | // (T()); - expression |
| 1456 | // |
| 1457 | // The bad news is that we cannot use the specialized tentative parser, since |
| 1458 | // it can only verify that the thing inside the parens can be parsed as |
| 1459 | // type-id, it is not useful for determining the context past the parens. |
| 1460 | // |
| 1461 | // The good news is that the parser can disambiguate this part without |
Argyrios Kyrtzidis | a558a89 | 2009-05-22 15:12:46 +0000 | [diff] [blame] | 1462 | // making any unnecessary Action calls. |
Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 1463 | // |
| 1464 | // It uses a scheme similar to parsing inline methods. The parenthesized |
| 1465 | // tokens are cached, the context that follows is determined (possibly by |
| 1466 | // parsing a cast-expression), and then we re-introduce the cached tokens |
| 1467 | // into the token stream and parse them appropriately. |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1468 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1469 | ParenParseOption ParseAs; |
Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 1470 | CachedTokens Toks; |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1471 | |
Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 1472 | // Store the tokens of the parentheses. We will parse them after we determine |
| 1473 | // the context that follows them. |
| 1474 | if (!ConsumeAndStoreUntil(tok::r_paren, tok::unknown, Toks, tok::semi)) { |
| 1475 | // We didn't find the ')' we expected. |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1476 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 1477 | return ExprError(); |
| 1478 | } |
| 1479 | |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1480 | if (Tok.is(tok::l_brace)) { |
Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 1481 | ParseAs = CompoundLiteral; |
| 1482 | } else { |
| 1483 | bool NotCastExpr; |
Eli Friedman | b53f08a | 2009-05-25 19:41:42 +0000 | [diff] [blame] | 1484 | // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression |
| 1485 | if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) { |
| 1486 | NotCastExpr = true; |
| 1487 | } else { |
| 1488 | // Try parsing the cast-expression that may follow. |
| 1489 | // If it is not a cast-expression, NotCastExpr will be true and no token |
| 1490 | // will be consumed. |
| 1491 | Result = ParseCastExpression(false/*isUnaryExpression*/, |
| 1492 | false/*isAddressofOperand*/, |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1493 | NotCastExpr, false); |
Eli Friedman | b53f08a | 2009-05-25 19:41:42 +0000 | [diff] [blame] | 1494 | } |
Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 1495 | |
| 1496 | // If we parsed a cast-expression, it's really a type-id, otherwise it's |
| 1497 | // an expression. |
| 1498 | ParseAs = NotCastExpr ? SimpleExpr : CastExpr; |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1501 | // The current token should go after the cached tokens. |
Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 1502 | Toks.push_back(Tok); |
| 1503 | // Re-enter the stored parenthesized tokens into the token stream, so we may |
| 1504 | // parse them now. |
| 1505 | PP.EnterTokenStream(Toks.data(), Toks.size(), |
| 1506 | true/*DisableMacroExpansion*/, false/*OwnsTokens*/); |
| 1507 | // Drop the current token and bring the first cached one. It's the same token |
| 1508 | // as when we entered this function. |
| 1509 | ConsumeAnyToken(); |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1510 | |
Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 1511 | if (ParseAs >= CompoundLiteral) { |
| 1512 | TypeResult Ty = ParseTypeName(); |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1513 | |
Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 1514 | // Match the ')'. |
| 1515 | if (Tok.is(tok::r_paren)) |
| 1516 | RParenLoc = ConsumeParen(); |
| 1517 | else |
| 1518 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1519 | |
Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 1520 | if (ParseAs == CompoundLiteral) { |
| 1521 | ExprType = CompoundLiteral; |
| 1522 | return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc); |
| 1523 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1524 | |
Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 1525 | // We parsed '(' type-id ')' and the thing after it wasn't a '{'. |
| 1526 | assert(ParseAs == CastExpr); |
| 1527 | |
| 1528 | if (Ty.isInvalid()) |
| 1529 | return ExprError(); |
| 1530 | |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1531 | CastTy = Ty.get(); |
Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 1532 | |
| 1533 | // Result is what ParseCastExpression returned earlier. |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1534 | if (!Result.isInvalid()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1535 | Result = Actions.ActOnCastExpr(CurScope, LParenLoc, CastTy, RParenLoc, |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1536 | move(Result)); |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1537 | return move(Result); |
| 1538 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1539 | |
Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 1540 | // Not a compound literal, and not followed by a cast-expression. |
| 1541 | assert(ParseAs == SimpleExpr); |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1542 | |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1543 | ExprType = SimpleExpr; |
Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 1544 | Result = ParseExpression(); |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1545 | if (!Result.isInvalid() && Tok.is(tok::r_paren)) |
| 1546 | Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), move(Result)); |
| 1547 | |
| 1548 | // Match the ')'. |
| 1549 | if (Result.isInvalid()) { |
| 1550 | SkipUntil(tok::r_paren); |
| 1551 | return ExprError(); |
| 1552 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1553 | |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1554 | if (Tok.is(tok::r_paren)) |
| 1555 | RParenLoc = ConsumeParen(); |
| 1556 | else |
| 1557 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 1558 | |
| 1559 | return move(Result); |
| 1560 | } |