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" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 17 | using namespace clang; |
| 18 | |
Chris Lattner | 7a0ab5f | 2009-01-06 06:59:53 +0000 | [diff] [blame] | 19 | /// ParseOptionalCXXScopeSpecifier - Parse global scope or |
| 20 | /// nested-name-specifier if present. Returns true if a nested-name-specifier |
| 21 | /// was parsed from the token stream. Note that this routine will not parse |
| 22 | /// ::new or ::delete, it will just leave them in the token stream. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 23 | /// |
| 24 | /// '::'[opt] nested-name-specifier |
| 25 | /// '::' |
| 26 | /// |
| 27 | /// nested-name-specifier: |
| 28 | /// type-name '::' |
| 29 | /// namespace-name '::' |
| 30 | /// nested-name-specifier identifier '::' |
| 31 | /// nested-name-specifier 'template'[opt] simple-template-id '::' [TODO] |
| 32 | /// |
Chris Lattner | 7a0ab5f | 2009-01-06 06:59:53 +0000 | [diff] [blame] | 33 | bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS) { |
Argyrios Kyrtzidis | 4bdd91c | 2008-11-26 21:41:52 +0000 | [diff] [blame] | 34 | assert(getLang().CPlusPlus && |
Chris Lattner | 7452c6f | 2009-01-05 01:24:05 +0000 | [diff] [blame] | 35 | "Call sites of this function should be guarded by checking for C++"); |
Argyrios Kyrtzidis | 4bdd91c | 2008-11-26 21:41:52 +0000 | [diff] [blame] | 36 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 37 | if (Tok.is(tok::annot_cxxscope)) { |
Douglas Gregor | 3507369 | 2009-03-26 23:56:24 +0000 | [diff] [blame] | 38 | SS.setScopeRep(Tok.getAnnotationValue()); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 39 | SS.setRange(Tok.getAnnotationRange()); |
| 40 | ConsumeToken(); |
Argyrios Kyrtzidis | 4bdd91c | 2008-11-26 21:41:52 +0000 | [diff] [blame] | 41 | return true; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 42 | } |
Chris Lattner | e607e80 | 2009-01-04 21:14:15 +0000 | [diff] [blame] | 43 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 44 | bool HasScopeSpecifier = false; |
| 45 | |
Chris Lattner | 5b45473 | 2009-01-05 03:55:46 +0000 | [diff] [blame] | 46 | if (Tok.is(tok::coloncolon)) { |
| 47 | // ::new and ::delete aren't nested-name-specifiers. |
| 48 | tok::TokenKind NextKind = NextToken().getKind(); |
| 49 | if (NextKind == tok::kw_new || NextKind == tok::kw_delete) |
| 50 | return false; |
Chris Lattner | 55a7cef | 2009-01-05 00:13:00 +0000 | [diff] [blame] | 51 | |
Chris Lattner | 55a7cef | 2009-01-05 00:13:00 +0000 | [diff] [blame] | 52 | // '::' - Global scope qualifier. |
Chris Lattner | 357089d | 2009-01-05 02:07:19 +0000 | [diff] [blame] | 53 | SourceLocation CCLoc = ConsumeToken(); |
Chris Lattner | 357089d | 2009-01-05 02:07:19 +0000 | [diff] [blame] | 54 | SS.setBeginLoc(CCLoc); |
Douglas Gregor | 3507369 | 2009-03-26 23:56:24 +0000 | [diff] [blame] | 55 | SS.setScopeRep(Actions.ActOnCXXGlobalScopeSpecifier(CurScope, CCLoc)); |
Chris Lattner | 357089d | 2009-01-05 02:07:19 +0000 | [diff] [blame] | 56 | SS.setEndLoc(CCLoc); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 57 | HasScopeSpecifier = true; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 60 | while (true) { |
| 61 | // nested-name-specifier: |
Chris Lattner | 77cf72a | 2009-06-26 03:47:46 +0000 | [diff] [blame] | 62 | // nested-name-specifier 'template'[opt] simple-template-id '::' |
| 63 | |
| 64 | // Parse the optional 'template' keyword, then make sure we have |
| 65 | // 'identifier <' after it. |
| 66 | if (Tok.is(tok::kw_template)) { |
| 67 | SourceLocation TemplateKWLoc = ConsumeToken(); |
| 68 | |
| 69 | if (Tok.isNot(tok::identifier)) { |
| 70 | Diag(Tok.getLocation(), |
| 71 | diag::err_id_after_template_in_nested_name_spec) |
| 72 | << SourceRange(TemplateKWLoc); |
| 73 | break; |
| 74 | } |
| 75 | |
| 76 | if (NextToken().isNot(tok::less)) { |
| 77 | Diag(NextToken().getLocation(), |
| 78 | diag::err_less_after_template_name_in_nested_name_spec) |
| 79 | << Tok.getIdentifierInfo()->getName() |
| 80 | << SourceRange(TemplateKWLoc, Tok.getLocation()); |
| 81 | break; |
| 82 | } |
| 83 | |
| 84 | TemplateTy Template |
| 85 | = Actions.ActOnDependentTemplateName(TemplateKWLoc, |
| 86 | *Tok.getIdentifierInfo(), |
| 87 | Tok.getLocation(), SS); |
| 88 | AnnotateTemplateIdToken(Template, TNK_Dependent_template_name, |
| 89 | &SS, TemplateKWLoc, false); |
| 90 | continue; |
| 91 | } |
| 92 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 93 | if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) { |
| 94 | // We have |
| 95 | // |
| 96 | // simple-template-id '::' |
| 97 | // |
| 98 | // 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] | 99 | // right kind (it should name a type or be dependent), and then |
| 100 | // convert it into a type within the nested-name-specifier. |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 101 | TemplateIdAnnotation *TemplateId |
| 102 | = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); |
| 103 | |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 104 | if (TemplateId->Kind == TNK_Type_template || |
| 105 | TemplateId->Kind == TNK_Dependent_template_name) { |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 106 | AnnotateTemplateIdTokenAsType(&SS); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 107 | SS.setScopeRep(0); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 108 | |
| 109 | assert(Tok.is(tok::annot_typename) && |
| 110 | "AnnotateTemplateIdTokenAsType isn't working"); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 111 | Token TypeToken = Tok; |
| 112 | ConsumeToken(); |
| 113 | assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!"); |
| 114 | SourceLocation CCLoc = ConsumeToken(); |
| 115 | |
| 116 | if (!HasScopeSpecifier) { |
| 117 | SS.setBeginLoc(TypeToken.getLocation()); |
| 118 | HasScopeSpecifier = true; |
| 119 | } |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 120 | |
| 121 | if (TypeToken.getAnnotationValue()) |
| 122 | SS.setScopeRep( |
| 123 | Actions.ActOnCXXNestedNameSpecifier(CurScope, SS, |
| 124 | TypeToken.getAnnotationValue(), |
| 125 | TypeToken.getAnnotationRange(), |
| 126 | CCLoc)); |
| 127 | else |
| 128 | SS.setScopeRep(0); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 129 | SS.setEndLoc(CCLoc); |
| 130 | continue; |
Chris Lattner | 67b9e83 | 2009-06-26 03:45:46 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | assert(false && "FIXME: Only type template names supported here"); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Chris Lattner | 5c7f786 | 2009-06-26 03:52:38 +0000 | [diff] [blame^] | 136 | |
| 137 | // The rest of the nested-name-specifier possibilities start with |
| 138 | // tok::identifier. |
| 139 | if (Tok.isNot(tok::identifier)) |
| 140 | break; |
| 141 | |
| 142 | IdentifierInfo &II = *Tok.getIdentifierInfo(); |
| 143 | |
| 144 | // nested-name-specifier: |
| 145 | // type-name '::' |
| 146 | // namespace-name '::' |
| 147 | // nested-name-specifier identifier '::' |
| 148 | Token Next = NextToken(); |
| 149 | if (Next.is(tok::coloncolon)) { |
| 150 | // We have an identifier followed by a '::'. Lookup this name |
| 151 | // as the name in a nested-name-specifier. |
| 152 | SourceLocation IdLoc = ConsumeToken(); |
| 153 | assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!"); |
| 154 | SourceLocation CCLoc = ConsumeToken(); |
| 155 | |
| 156 | if (!HasScopeSpecifier) { |
| 157 | SS.setBeginLoc(IdLoc); |
| 158 | HasScopeSpecifier = true; |
| 159 | } |
| 160 | |
| 161 | if (SS.isInvalid()) |
| 162 | continue; |
| 163 | |
| 164 | SS.setScopeRep( |
| 165 | Actions.ActOnCXXNestedNameSpecifier(CurScope, SS, IdLoc, CCLoc, II)); |
| 166 | SS.setEndLoc(CCLoc); |
| 167 | continue; |
| 168 | } |
| 169 | |
| 170 | // nested-name-specifier: |
| 171 | // type-name '<' |
| 172 | if (Next.is(tok::less)) { |
| 173 | TemplateTy Template; |
| 174 | if (TemplateNameKind TNK = Actions.isTemplateName(II, CurScope, |
| 175 | Template, &SS)) { |
| 176 | // We have found a template name, so annotate this this token |
| 177 | // with a template-id annotation. We do not permit the |
| 178 | // template-id to be translated into a type annotation, |
| 179 | // because some clients (e.g., the parsing of class template |
| 180 | // specializations) still want to see the original template-id |
| 181 | // token. |
| 182 | AnnotateTemplateIdToken(Template, TNK, &SS, SourceLocation(), false); |
| 183 | continue; |
| 184 | } |
| 185 | } |
| 186 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 187 | // We don't have any tokens that form the beginning of a |
| 188 | // nested-name-specifier, so we're done. |
| 189 | break; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 190 | } |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 191 | |
| 192 | return HasScopeSpecifier; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /// ParseCXXIdExpression - Handle id-expression. |
| 196 | /// |
| 197 | /// id-expression: |
| 198 | /// unqualified-id |
| 199 | /// qualified-id |
| 200 | /// |
| 201 | /// unqualified-id: |
| 202 | /// identifier |
| 203 | /// operator-function-id |
| 204 | /// conversion-function-id [TODO] |
| 205 | /// '~' class-name [TODO] |
| 206 | /// template-id [TODO] |
| 207 | /// |
| 208 | /// qualified-id: |
| 209 | /// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id |
| 210 | /// '::' identifier |
| 211 | /// '::' operator-function-id |
| 212 | /// '::' template-id [TODO] |
| 213 | /// |
| 214 | /// nested-name-specifier: |
| 215 | /// type-name '::' |
| 216 | /// namespace-name '::' |
| 217 | /// nested-name-specifier identifier '::' |
| 218 | /// nested-name-specifier 'template'[opt] simple-template-id '::' [TODO] |
| 219 | /// |
| 220 | /// NOTE: The standard specifies that, for qualified-id, the parser does not |
| 221 | /// expect: |
| 222 | /// |
| 223 | /// '::' conversion-function-id |
| 224 | /// '::' '~' class-name |
| 225 | /// |
| 226 | /// This may cause a slight inconsistency on diagnostics: |
| 227 | /// |
| 228 | /// class C {}; |
| 229 | /// namespace A {} |
| 230 | /// void f() { |
| 231 | /// :: A :: ~ C(); // Some Sema error about using destructor with a |
| 232 | /// // namespace. |
| 233 | /// :: ~ C(); // Some Parser error like 'unexpected ~'. |
| 234 | /// } |
| 235 | /// |
| 236 | /// We simplify the parser a bit and make it work like: |
| 237 | /// |
| 238 | /// qualified-id: |
| 239 | /// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id |
| 240 | /// '::' unqualified-id |
| 241 | /// |
| 242 | /// That way Sema can handle and report similar errors for namespaces and the |
| 243 | /// global scope. |
| 244 | /// |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 245 | /// The isAddressOfOperand parameter indicates that this id-expression is a |
| 246 | /// direct operand of the address-of operator. This is, besides member contexts, |
| 247 | /// the only place where a qualified-id naming a non-static class member may |
| 248 | /// appear. |
| 249 | /// |
| 250 | Parser::OwningExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) { |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 251 | // qualified-id: |
| 252 | // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id |
| 253 | // '::' unqualified-id |
| 254 | // |
| 255 | CXXScopeSpec SS; |
Chris Lattner | 7a0ab5f | 2009-01-06 06:59:53 +0000 | [diff] [blame] | 256 | ParseOptionalCXXScopeSpecifier(SS); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 257 | |
| 258 | // unqualified-id: |
| 259 | // identifier |
| 260 | // operator-function-id |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 261 | // conversion-function-id |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 262 | // '~' class-name [TODO] |
| 263 | // template-id [TODO] |
| 264 | // |
| 265 | switch (Tok.getKind()) { |
| 266 | default: |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 267 | return ExprError(Diag(Tok, diag::err_expected_unqualified_id)); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 268 | |
| 269 | case tok::identifier: { |
| 270 | // Consume the identifier so that we can see if it is followed by a '('. |
| 271 | IdentifierInfo &II = *Tok.getIdentifierInfo(); |
| 272 | SourceLocation L = ConsumeToken(); |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 273 | return Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren), |
| 274 | &SS, isAddressOfOperand); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | case tok::kw_operator: { |
| 278 | SourceLocation OperatorLoc = Tok.getLocation(); |
Chris Lattner | 7452c6f | 2009-01-05 01:24:05 +0000 | [diff] [blame] | 279 | if (OverloadedOperatorKind Op = TryParseOperatorFunctionId()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 280 | return Actions.ActOnCXXOperatorFunctionIdExpr( |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 281 | CurScope, OperatorLoc, Op, Tok.is(tok::l_paren), SS, |
| 282 | isAddressOfOperand); |
Chris Lattner | 7452c6f | 2009-01-05 01:24:05 +0000 | [diff] [blame] | 283 | if (TypeTy *Type = ParseConversionFunctionId()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 284 | return Actions.ActOnCXXConversionFunctionExpr(CurScope, OperatorLoc, Type, |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 285 | Tok.is(tok::l_paren), SS, |
| 286 | isAddressOfOperand); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 287 | |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 288 | // We already complained about a bad conversion-function-id, |
| 289 | // above. |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 290 | return ExprError(); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | } // switch. |
| 294 | |
| 295 | assert(0 && "The switch was supposed to take care everything."); |
| 296 | } |
| 297 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 298 | /// ParseCXXCasts - This handles the various ways to cast expressions to another |
| 299 | /// type. |
| 300 | /// |
| 301 | /// postfix-expression: [C++ 5.2p1] |
| 302 | /// 'dynamic_cast' '<' type-name '>' '(' expression ')' |
| 303 | /// 'static_cast' '<' type-name '>' '(' expression ')' |
| 304 | /// 'reinterpret_cast' '<' type-name '>' '(' expression ')' |
| 305 | /// 'const_cast' '<' type-name '>' '(' expression ')' |
| 306 | /// |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 307 | Parser::OwningExprResult Parser::ParseCXXCasts() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 308 | tok::TokenKind Kind = Tok.getKind(); |
| 309 | const char *CastName = 0; // For error messages |
| 310 | |
| 311 | switch (Kind) { |
| 312 | default: assert(0 && "Unknown C++ cast!"); abort(); |
| 313 | case tok::kw_const_cast: CastName = "const_cast"; break; |
| 314 | case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break; |
| 315 | case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break; |
| 316 | case tok::kw_static_cast: CastName = "static_cast"; break; |
| 317 | } |
| 318 | |
| 319 | SourceLocation OpLoc = ConsumeToken(); |
| 320 | SourceLocation LAngleBracketLoc = Tok.getLocation(); |
| 321 | |
| 322 | if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName)) |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 323 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 324 | |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 325 | TypeResult CastTy = ParseTypeName(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 326 | SourceLocation RAngleBracketLoc = Tok.getLocation(); |
| 327 | |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 328 | if (ExpectAndConsume(tok::greater, diag::err_expected_greater)) |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 329 | return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 330 | |
| 331 | SourceLocation LParenLoc = Tok.getLocation(), RParenLoc; |
| 332 | |
Argyrios Kyrtzidis | 21e7ad2 | 2009-05-22 10:23:16 +0000 | [diff] [blame] | 333 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName)) |
| 334 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 335 | |
Argyrios Kyrtzidis | 21e7ad2 | 2009-05-22 10:23:16 +0000 | [diff] [blame] | 336 | OwningExprResult Result = ParseExpression(); |
| 337 | |
| 338 | // Match the ')'. |
| 339 | if (Result.isInvalid()) |
| 340 | SkipUntil(tok::r_paren); |
| 341 | |
| 342 | if (Tok.is(tok::r_paren)) |
| 343 | RParenLoc = ConsumeParen(); |
| 344 | else |
| 345 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 346 | |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 347 | if (!Result.isInvalid() && !CastTy.isInvalid()) |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 348 | Result = Actions.ActOnCXXNamedCast(OpLoc, Kind, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 349 | LAngleBracketLoc, CastTy.get(), |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 350 | RAngleBracketLoc, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 351 | LParenLoc, move(Result), RParenLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 352 | |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 353 | return move(Result); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 356 | /// ParseCXXTypeid - This handles the C++ typeid expression. |
| 357 | /// |
| 358 | /// postfix-expression: [C++ 5.2p1] |
| 359 | /// 'typeid' '(' expression ')' |
| 360 | /// 'typeid' '(' type-id ')' |
| 361 | /// |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 362 | Parser::OwningExprResult Parser::ParseCXXTypeid() { |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 363 | assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!"); |
| 364 | |
| 365 | SourceLocation OpLoc = ConsumeToken(); |
| 366 | SourceLocation LParenLoc = Tok.getLocation(); |
| 367 | SourceLocation RParenLoc; |
| 368 | |
| 369 | // typeid expressions are always parenthesized. |
| 370 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 371 | "typeid")) |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 372 | return ExprError(); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 373 | |
Sebastian Redl | 15faa7f | 2008-12-09 20:22:58 +0000 | [diff] [blame] | 374 | OwningExprResult Result(Actions); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 375 | |
| 376 | if (isTypeIdInParens()) { |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 377 | TypeResult Ty = ParseTypeName(); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 378 | |
| 379 | // Match the ')'. |
| 380 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 381 | |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 382 | if (Ty.isInvalid()) |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 383 | return ExprError(); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 384 | |
| 385 | Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true, |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 386 | Ty.get(), RParenLoc); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 387 | } else { |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 388 | // C++0x [expr.typeid]p3: |
| 389 | // When typeid is applied to an expression other than an lvalue of a |
| 390 | // polymorphic class type [...] The expression is an unevaluated |
| 391 | // operand (Clause 5). |
| 392 | // |
| 393 | // Note that we can't tell whether the expression is an lvalue of a |
| 394 | // polymorphic class type until after we've parsed the expression, so |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 395 | // we the expression is potentially potentially evaluated. |
| 396 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 397 | Action::PotentiallyPotentiallyEvaluated); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 398 | Result = ParseExpression(); |
| 399 | |
| 400 | // Match the ')'. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 401 | if (Result.isInvalid()) |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 402 | SkipUntil(tok::r_paren); |
| 403 | else { |
| 404 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 405 | |
| 406 | Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false, |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 407 | Result.release(), RParenLoc); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 411 | return move(Result); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 414 | /// ParseCXXBoolLiteral - This handles the C++ Boolean literals. |
| 415 | /// |
| 416 | /// boolean-literal: [C++ 2.13.5] |
| 417 | /// 'true' |
| 418 | /// 'false' |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 419 | Parser::OwningExprResult Parser::ParseCXXBoolLiteral() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 420 | tok::TokenKind Kind = Tok.getKind(); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 421 | return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 422 | } |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 423 | |
| 424 | /// ParseThrowExpression - This handles the C++ throw expression. |
| 425 | /// |
| 426 | /// throw-expression: [C++ 15] |
| 427 | /// 'throw' assignment-expression[opt] |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 428 | Parser::OwningExprResult Parser::ParseThrowExpression() { |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 429 | assert(Tok.is(tok::kw_throw) && "Not throw!"); |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 430 | SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token. |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 431 | |
Chris Lattner | 2a2819a | 2008-04-06 06:02:23 +0000 | [diff] [blame] | 432 | // If the current token isn't the start of an assignment-expression, |
| 433 | // then the expression is not present. This handles things like: |
| 434 | // "C ? throw : (void)42", which is crazy but legal. |
| 435 | switch (Tok.getKind()) { // FIXME: move this predicate somewhere common. |
| 436 | case tok::semi: |
| 437 | case tok::r_paren: |
| 438 | case tok::r_square: |
| 439 | case tok::r_brace: |
| 440 | case tok::colon: |
| 441 | case tok::comma: |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 442 | return Actions.ActOnCXXThrow(ThrowLoc, ExprArg(Actions)); |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 443 | |
Chris Lattner | 2a2819a | 2008-04-06 06:02:23 +0000 | [diff] [blame] | 444 | default: |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 445 | OwningExprResult Expr(ParseAssignmentExpression()); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 446 | if (Expr.isInvalid()) return move(Expr); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 447 | return Actions.ActOnCXXThrow(ThrowLoc, move(Expr)); |
Chris Lattner | 2a2819a | 2008-04-06 06:02:23 +0000 | [diff] [blame] | 448 | } |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 449 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 450 | |
| 451 | /// ParseCXXThis - This handles the C++ 'this' pointer. |
| 452 | /// |
| 453 | /// C++ 9.3.2: In the body of a non-static member function, the keyword this is |
| 454 | /// a non-lvalue expression whose value is the address of the object for which |
| 455 | /// the function is called. |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 456 | Parser::OwningExprResult Parser::ParseCXXThis() { |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 457 | assert(Tok.is(tok::kw_this) && "Not 'this'!"); |
| 458 | SourceLocation ThisLoc = ConsumeToken(); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 459 | return Actions.ActOnCXXThis(ThisLoc); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 460 | } |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 461 | |
| 462 | /// ParseCXXTypeConstructExpression - Parse construction of a specified type. |
| 463 | /// Can be interpreted either as function-style casting ("int(x)") |
| 464 | /// or class type construction ("ClassType(x,y,z)") |
| 465 | /// or creation of a value-initialized type ("int()"). |
| 466 | /// |
| 467 | /// postfix-expression: [C++ 5.2p1] |
| 468 | /// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3] |
| 469 | /// typename-specifier '(' expression-list[opt] ')' [TODO] |
| 470 | /// |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 471 | Parser::OwningExprResult |
| 472 | Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) { |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 473 | Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); |
Douglas Gregor | 5ac8aff | 2009-01-26 22:44:13 +0000 | [diff] [blame] | 474 | TypeTy *TypeRep = Actions.ActOnTypeName(CurScope, DeclaratorInfo).get(); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 475 | |
| 476 | assert(Tok.is(tok::l_paren) && "Expected '('!"); |
| 477 | SourceLocation LParenLoc = ConsumeParen(); |
| 478 | |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 479 | ExprVector Exprs(Actions); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 480 | CommaLocsTy CommaLocs; |
| 481 | |
| 482 | if (Tok.isNot(tok::r_paren)) { |
| 483 | if (ParseExpressionList(Exprs, CommaLocs)) { |
| 484 | SkipUntil(tok::r_paren); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 485 | return ExprError(); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 486 | } |
| 487 | } |
| 488 | |
| 489 | // Match the ')'. |
| 490 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 491 | |
| 492 | assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&& |
| 493 | "Unexpected number of commas!"); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 494 | return Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep, |
| 495 | LParenLoc, move_arg(Exprs), |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 496 | CommaLocs.data(), RParenLoc); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 499 | /// ParseCXXCondition - if/switch/while/for condition expression. |
| 500 | /// |
| 501 | /// condition: |
| 502 | /// expression |
| 503 | /// type-specifier-seq declarator '=' assignment-expression |
| 504 | /// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt] |
| 505 | /// '=' assignment-expression |
| 506 | /// |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 507 | Parser::OwningExprResult Parser::ParseCXXCondition() { |
Argyrios Kyrtzidis | a8a4598 | 2008-10-05 15:03:47 +0000 | [diff] [blame] | 508 | if (!isCXXConditionDeclaration()) |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 509 | return ParseExpression(); // expression |
| 510 | |
| 511 | SourceLocation StartLoc = Tok.getLocation(); |
| 512 | |
| 513 | // type-specifier-seq |
| 514 | DeclSpec DS; |
| 515 | ParseSpecifierQualifierList(DS); |
| 516 | |
| 517 | // declarator |
| 518 | Declarator DeclaratorInfo(DS, Declarator::ConditionContext); |
| 519 | ParseDeclarator(DeclaratorInfo); |
| 520 | |
| 521 | // simple-asm-expr[opt] |
| 522 | if (Tok.is(tok::kw_asm)) { |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 523 | SourceLocation Loc; |
| 524 | OwningExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 525 | if (AsmLabel.isInvalid()) { |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 526 | SkipUntil(tok::semi); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 527 | return ExprError(); |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 528 | } |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 529 | DeclaratorInfo.setAsmLabel(AsmLabel.release()); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 530 | DeclaratorInfo.SetRangeEnd(Loc); |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | // If attributes are present, parse them. |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 534 | if (Tok.is(tok::kw___attribute)) { |
| 535 | SourceLocation Loc; |
| 536 | AttributeList *AttrList = ParseAttributes(&Loc); |
| 537 | DeclaratorInfo.AddAttributes(AttrList, Loc); |
| 538 | } |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 539 | |
| 540 | // '=' assignment-expression |
| 541 | if (Tok.isNot(tok::equal)) |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 542 | return ExprError(Diag(Tok, diag::err_expected_equal_after_declarator)); |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 543 | SourceLocation EqualLoc = ConsumeToken(); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 544 | OwningExprResult AssignExpr(ParseAssignmentExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 545 | if (AssignExpr.isInvalid()) |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 546 | return ExprError(); |
| 547 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 548 | return Actions.ActOnCXXConditionDeclarationExpr(CurScope, StartLoc, |
| 549 | DeclaratorInfo,EqualLoc, |
| 550 | move(AssignExpr)); |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 553 | /// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers. |
| 554 | /// This should only be called when the current token is known to be part of |
| 555 | /// simple-type-specifier. |
| 556 | /// |
| 557 | /// simple-type-specifier: |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 558 | /// '::'[opt] nested-name-specifier[opt] type-name |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 559 | /// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO] |
| 560 | /// char |
| 561 | /// wchar_t |
| 562 | /// bool |
| 563 | /// short |
| 564 | /// int |
| 565 | /// long |
| 566 | /// signed |
| 567 | /// unsigned |
| 568 | /// float |
| 569 | /// double |
| 570 | /// void |
| 571 | /// [GNU] typeof-specifier |
| 572 | /// [C++0x] auto [TODO] |
| 573 | /// |
| 574 | /// type-name: |
| 575 | /// class-name |
| 576 | /// enum-name |
| 577 | /// typedef-name |
| 578 | /// |
| 579 | void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) { |
| 580 | DS.SetRangeStart(Tok.getLocation()); |
| 581 | const char *PrevSpec; |
| 582 | SourceLocation Loc = Tok.getLocation(); |
| 583 | |
| 584 | switch (Tok.getKind()) { |
Chris Lattner | 55a7cef | 2009-01-05 00:13:00 +0000 | [diff] [blame] | 585 | case tok::identifier: // foo::bar |
| 586 | case tok::coloncolon: // ::foo::bar |
| 587 | assert(0 && "Annotation token should already be formed!"); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 588 | default: |
| 589 | assert(0 && "Not a simple-type-specifier token!"); |
| 590 | abort(); |
Chris Lattner | 55a7cef | 2009-01-05 00:13:00 +0000 | [diff] [blame] | 591 | |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 592 | // type-name |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 593 | case tok::annot_typename: { |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 594 | DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 595 | Tok.getAnnotationValue()); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 596 | break; |
| 597 | } |
| 598 | |
| 599 | // builtin types |
| 600 | case tok::kw_short: |
| 601 | DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec); |
| 602 | break; |
| 603 | case tok::kw_long: |
| 604 | DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec); |
| 605 | break; |
| 606 | case tok::kw_signed: |
| 607 | DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec); |
| 608 | break; |
| 609 | case tok::kw_unsigned: |
| 610 | DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec); |
| 611 | break; |
| 612 | case tok::kw_void: |
| 613 | DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec); |
| 614 | break; |
| 615 | case tok::kw_char: |
| 616 | DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec); |
| 617 | break; |
| 618 | case tok::kw_int: |
| 619 | DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec); |
| 620 | break; |
| 621 | case tok::kw_float: |
| 622 | DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec); |
| 623 | break; |
| 624 | case tok::kw_double: |
| 625 | DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec); |
| 626 | break; |
| 627 | case tok::kw_wchar_t: |
| 628 | DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec); |
| 629 | break; |
| 630 | case tok::kw_bool: |
| 631 | DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec); |
| 632 | break; |
| 633 | |
| 634 | // GNU typeof support. |
| 635 | case tok::kw_typeof: |
| 636 | ParseTypeofSpecifier(DS); |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 637 | DS.Finish(Diags, PP); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 638 | return; |
| 639 | } |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 640 | if (Tok.is(tok::annot_typename)) |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 641 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 642 | else |
| 643 | DS.SetRangeEnd(Tok.getLocation()); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 644 | ConsumeToken(); |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 645 | DS.Finish(Diags, PP); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 646 | } |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 647 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 648 | /// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++ |
| 649 | /// [dcl.name]), which is a non-empty sequence of type-specifiers, |
| 650 | /// e.g., "const short int". Note that the DeclSpec is *not* finished |
| 651 | /// by parsing the type-specifier-seq, because these sequences are |
| 652 | /// typically followed by some form of declarator. Returns true and |
| 653 | /// emits diagnostics if this is not a type-specifier-seq, false |
| 654 | /// otherwise. |
| 655 | /// |
| 656 | /// type-specifier-seq: [C++ 8.1] |
| 657 | /// type-specifier type-specifier-seq[opt] |
| 658 | /// |
| 659 | bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) { |
| 660 | DS.SetRangeStart(Tok.getLocation()); |
| 661 | const char *PrevSpec = 0; |
| 662 | int isInvalid = 0; |
| 663 | |
| 664 | // Parse one or more of the type specifiers. |
Chris Lattner | 7a0ab5f | 2009-01-06 06:59:53 +0000 | [diff] [blame] | 665 | if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 666 | Diag(Tok, diag::err_operator_missing_type_specifier); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 667 | return true; |
| 668 | } |
Chris Lattner | 7a0ab5f | 2009-01-06 06:59:53 +0000 | [diff] [blame] | 669 | |
Ted Kremenek | b8006e5 | 2009-01-06 19:17:58 +0000 | [diff] [blame] | 670 | while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec)) ; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 671 | |
| 672 | return false; |
| 673 | } |
| 674 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 675 | /// TryParseOperatorFunctionId - Attempts to parse a C++ overloaded |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 676 | /// operator name (C++ [over.oper]). If successful, returns the |
| 677 | /// predefined identifier that corresponds to that overloaded |
| 678 | /// operator. Otherwise, returns NULL and does not consume any tokens. |
| 679 | /// |
| 680 | /// operator-function-id: [C++ 13.5] |
| 681 | /// 'operator' operator |
| 682 | /// |
| 683 | /// operator: one of |
| 684 | /// new delete new[] delete[] |
| 685 | /// + - * / % ^ & | ~ |
| 686 | /// ! = < > += -= *= /= %= |
| 687 | /// ^= &= |= << >> >>= <<= == != |
| 688 | /// <= >= && || ++ -- , ->* -> |
| 689 | /// () [] |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 690 | OverloadedOperatorKind |
| 691 | Parser::TryParseOperatorFunctionId(SourceLocation *EndLoc) { |
Argyrios Kyrtzidis | 9057a81 | 2008-11-07 15:54:02 +0000 | [diff] [blame] | 692 | assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword"); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 693 | SourceLocation Loc; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 694 | |
| 695 | OverloadedOperatorKind Op = OO_None; |
| 696 | switch (NextToken().getKind()) { |
| 697 | case tok::kw_new: |
| 698 | ConsumeToken(); // 'operator' |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 699 | Loc = ConsumeToken(); // 'new' |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 700 | if (Tok.is(tok::l_square)) { |
| 701 | ConsumeBracket(); // '[' |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 702 | Loc = Tok.getLocation(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 703 | ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']' |
| 704 | Op = OO_Array_New; |
| 705 | } else { |
| 706 | Op = OO_New; |
| 707 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 708 | if (EndLoc) |
| 709 | *EndLoc = Loc; |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 710 | return Op; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 711 | |
| 712 | case tok::kw_delete: |
| 713 | ConsumeToken(); // 'operator' |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 714 | Loc = ConsumeToken(); // 'delete' |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 715 | if (Tok.is(tok::l_square)) { |
| 716 | ConsumeBracket(); // '[' |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 717 | Loc = Tok.getLocation(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 718 | ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']' |
| 719 | Op = OO_Array_Delete; |
| 720 | } else { |
| 721 | Op = OO_Delete; |
| 722 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 723 | if (EndLoc) |
| 724 | *EndLoc = Loc; |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 725 | return Op; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 726 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 727 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 728 | case tok::Token: Op = OO_##Name; break; |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 729 | #define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly) |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 730 | #include "clang/Basic/OperatorKinds.def" |
| 731 | |
| 732 | case tok::l_paren: |
| 733 | ConsumeToken(); // 'operator' |
| 734 | ConsumeParen(); // '(' |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 735 | Loc = Tok.getLocation(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 736 | ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); // ')' |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 737 | if (EndLoc) |
| 738 | *EndLoc = Loc; |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 739 | return OO_Call; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 740 | |
| 741 | case tok::l_square: |
| 742 | ConsumeToken(); // 'operator' |
| 743 | ConsumeBracket(); // '[' |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 744 | Loc = Tok.getLocation(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 745 | ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']' |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 746 | if (EndLoc) |
| 747 | *EndLoc = Loc; |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 748 | return OO_Subscript; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 749 | |
| 750 | default: |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 751 | return OO_None; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 752 | } |
| 753 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 754 | ConsumeToken(); // 'operator' |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 755 | Loc = ConsumeAnyToken(); // the operator itself |
| 756 | if (EndLoc) |
| 757 | *EndLoc = Loc; |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 758 | return Op; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 759 | } |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 760 | |
| 761 | /// ParseConversionFunctionId - Parse a C++ conversion-function-id, |
| 762 | /// which expresses the name of a user-defined conversion operator |
| 763 | /// (C++ [class.conv.fct]p1). Returns the type that this operator is |
| 764 | /// specifying a conversion for, or NULL if there was an error. |
| 765 | /// |
| 766 | /// conversion-function-id: [C++ 12.3.2] |
| 767 | /// operator conversion-type-id |
| 768 | /// |
| 769 | /// conversion-type-id: |
| 770 | /// type-specifier-seq conversion-declarator[opt] |
| 771 | /// |
| 772 | /// conversion-declarator: |
| 773 | /// ptr-operator conversion-declarator[opt] |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 774 | Parser::TypeTy *Parser::ParseConversionFunctionId(SourceLocation *EndLoc) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 775 | assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword"); |
| 776 | ConsumeToken(); // 'operator' |
| 777 | |
| 778 | // Parse the type-specifier-seq. |
| 779 | DeclSpec DS; |
| 780 | if (ParseCXXTypeSpecifierSeq(DS)) |
| 781 | return 0; |
| 782 | |
| 783 | // Parse the conversion-declarator, which is merely a sequence of |
| 784 | // ptr-operators. |
| 785 | Declarator D(DS, Declarator::TypeNameContext); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 786 | ParseDeclaratorInternal(D, /*DirectDeclParser=*/0); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 787 | if (EndLoc) |
| 788 | *EndLoc = D.getSourceRange().getEnd(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 789 | |
| 790 | // Finish up the type. |
| 791 | Action::TypeResult Result = Actions.ActOnTypeName(CurScope, D); |
Douglas Gregor | 5ac8aff | 2009-01-26 22:44:13 +0000 | [diff] [blame] | 792 | if (Result.isInvalid()) |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 793 | return 0; |
| 794 | else |
Douglas Gregor | 5ac8aff | 2009-01-26 22:44:13 +0000 | [diff] [blame] | 795 | return Result.get(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 796 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 797 | |
| 798 | /// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate |
| 799 | /// memory in a typesafe manner and call constructors. |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 800 | /// |
| 801 | /// This method is called to parse the new expression after the optional :: has |
| 802 | /// been already parsed. If the :: was present, "UseGlobal" is true and "Start" |
| 803 | /// is its location. Otherwise, "Start" is the location of the 'new' token. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 804 | /// |
| 805 | /// new-expression: |
| 806 | /// '::'[opt] 'new' new-placement[opt] new-type-id |
| 807 | /// new-initializer[opt] |
| 808 | /// '::'[opt] 'new' new-placement[opt] '(' type-id ')' |
| 809 | /// new-initializer[opt] |
| 810 | /// |
| 811 | /// new-placement: |
| 812 | /// '(' expression-list ')' |
| 813 | /// |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 814 | /// new-type-id: |
| 815 | /// type-specifier-seq new-declarator[opt] |
| 816 | /// |
| 817 | /// new-declarator: |
| 818 | /// ptr-operator new-declarator[opt] |
| 819 | /// direct-new-declarator |
| 820 | /// |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 821 | /// new-initializer: |
| 822 | /// '(' expression-list[opt] ')' |
| 823 | /// [C++0x] braced-init-list [TODO] |
| 824 | /// |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 825 | Parser::OwningExprResult |
| 826 | Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) { |
| 827 | assert(Tok.is(tok::kw_new) && "expected 'new' token"); |
| 828 | ConsumeToken(); // Consume 'new' |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 829 | |
| 830 | // A '(' now can be a new-placement or the '(' wrapping the type-id in the |
| 831 | // second form of new-expression. It can't be a new-type-id. |
| 832 | |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 833 | ExprVector PlacementArgs(Actions); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 834 | SourceLocation PlacementLParen, PlacementRParen; |
| 835 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 836 | bool ParenTypeId; |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 837 | DeclSpec DS; |
| 838 | Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 839 | if (Tok.is(tok::l_paren)) { |
| 840 | // If it turns out to be a placement, we change the type location. |
| 841 | PlacementLParen = ConsumeParen(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 842 | if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) { |
| 843 | SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 844 | return ExprError(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 845 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 846 | |
| 847 | PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 848 | if (PlacementRParen.isInvalid()) { |
| 849 | SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 850 | return ExprError(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 851 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 852 | |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 853 | if (PlacementArgs.empty()) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 854 | // Reset the placement locations. There was no placement. |
| 855 | PlacementLParen = PlacementRParen = SourceLocation(); |
| 856 | ParenTypeId = true; |
| 857 | } else { |
| 858 | // We still need the type. |
| 859 | if (Tok.is(tok::l_paren)) { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 860 | SourceLocation LParen = ConsumeParen(); |
| 861 | ParseSpecifierQualifierList(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 862 | DeclaratorInfo.SetSourceRange(DS.getSourceRange()); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 863 | ParseDeclarator(DeclaratorInfo); |
| 864 | MatchRHSPunctuation(tok::r_paren, LParen); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 865 | ParenTypeId = true; |
| 866 | } else { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 867 | if (ParseCXXTypeSpecifierSeq(DS)) |
| 868 | DeclaratorInfo.setInvalidType(true); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 869 | else { |
| 870 | DeclaratorInfo.SetSourceRange(DS.getSourceRange()); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 871 | ParseDeclaratorInternal(DeclaratorInfo, |
| 872 | &Parser::ParseDirectNewDeclarator); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 873 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 874 | ParenTypeId = false; |
| 875 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 876 | } |
| 877 | } else { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 878 | // A new-type-id is a simplified type-id, where essentially the |
| 879 | // direct-declarator is replaced by a direct-new-declarator. |
| 880 | if (ParseCXXTypeSpecifierSeq(DS)) |
| 881 | DeclaratorInfo.setInvalidType(true); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 882 | else { |
| 883 | DeclaratorInfo.SetSourceRange(DS.getSourceRange()); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 884 | ParseDeclaratorInternal(DeclaratorInfo, |
| 885 | &Parser::ParseDirectNewDeclarator); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 886 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 887 | ParenTypeId = false; |
| 888 | } |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 889 | if (DeclaratorInfo.isInvalidType()) { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 890 | SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 891 | return ExprError(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 892 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 893 | |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 894 | ExprVector ConstructorArgs(Actions); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 895 | SourceLocation ConstructorLParen, ConstructorRParen; |
| 896 | |
| 897 | if (Tok.is(tok::l_paren)) { |
| 898 | ConstructorLParen = ConsumeParen(); |
| 899 | if (Tok.isNot(tok::r_paren)) { |
| 900 | CommaLocsTy CommaLocs; |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 901 | if (ParseExpressionList(ConstructorArgs, CommaLocs)) { |
| 902 | SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 903 | return ExprError(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 904 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 905 | } |
| 906 | ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 907 | if (ConstructorRParen.isInvalid()) { |
| 908 | SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 909 | return ExprError(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 910 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 911 | } |
| 912 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 913 | return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen, |
| 914 | move_arg(PlacementArgs), PlacementRParen, |
| 915 | ParenTypeId, DeclaratorInfo, ConstructorLParen, |
| 916 | move_arg(ConstructorArgs), ConstructorRParen); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 917 | } |
| 918 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 919 | /// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be |
| 920 | /// passed to ParseDeclaratorInternal. |
| 921 | /// |
| 922 | /// direct-new-declarator: |
| 923 | /// '[' expression ']' |
| 924 | /// direct-new-declarator '[' constant-expression ']' |
| 925 | /// |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 926 | void Parser::ParseDirectNewDeclarator(Declarator &D) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 927 | // Parse the array dimensions. |
| 928 | bool first = true; |
| 929 | while (Tok.is(tok::l_square)) { |
| 930 | SourceLocation LLoc = ConsumeBracket(); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 931 | OwningExprResult Size(first ? ParseExpression() |
| 932 | : ParseConstantExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 933 | if (Size.isInvalid()) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 934 | // Recover |
| 935 | SkipUntil(tok::r_square); |
| 936 | return; |
| 937 | } |
| 938 | first = false; |
| 939 | |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 940 | SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 941 | D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false, |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 942 | Size.release(), LLoc), |
| 943 | RLoc); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 944 | |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 945 | if (RLoc.isInvalid()) |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 946 | return; |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | /// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id. |
| 951 | /// This ambiguity appears in the syntax of the C++ new operator. |
| 952 | /// |
| 953 | /// new-expression: |
| 954 | /// '::'[opt] 'new' new-placement[opt] '(' type-id ')' |
| 955 | /// new-initializer[opt] |
| 956 | /// |
| 957 | /// new-placement: |
| 958 | /// '(' expression-list ')' |
| 959 | /// |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 960 | bool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs, |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 961 | Declarator &D) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 962 | // The '(' was already consumed. |
| 963 | if (isTypeIdInParens()) { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 964 | ParseSpecifierQualifierList(D.getMutableDeclSpec()); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 965 | D.SetSourceRange(D.getDeclSpec().getSourceRange()); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 966 | ParseDeclarator(D); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 967 | return D.isInvalidType(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | // It's not a type, it has to be an expression list. |
| 971 | // Discard the comma locations - ActOnCXXNew has enough parameters. |
| 972 | CommaLocsTy CommaLocs; |
| 973 | return ParseExpressionList(PlacementArgs, CommaLocs); |
| 974 | } |
| 975 | |
| 976 | /// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used |
| 977 | /// to free memory allocated by new. |
| 978 | /// |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 979 | /// This method is called to parse the 'delete' expression after the optional |
| 980 | /// '::' has been already parsed. If the '::' was present, "UseGlobal" is true |
| 981 | /// and "Start" is its location. Otherwise, "Start" is the location of the |
| 982 | /// 'delete' token. |
| 983 | /// |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 984 | /// delete-expression: |
| 985 | /// '::'[opt] 'delete' cast-expression |
| 986 | /// '::'[opt] 'delete' '[' ']' cast-expression |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 987 | Parser::OwningExprResult |
| 988 | Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) { |
| 989 | assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword"); |
| 990 | ConsumeToken(); // Consume 'delete' |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 991 | |
| 992 | // Array delete? |
| 993 | bool ArrayDelete = false; |
| 994 | if (Tok.is(tok::l_square)) { |
| 995 | ArrayDelete = true; |
| 996 | SourceLocation LHS = ConsumeBracket(); |
| 997 | SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS); |
| 998 | if (RHS.isInvalid()) |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 999 | return ExprError(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 1002 | OwningExprResult Operand(ParseCastExpression(false)); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1003 | if (Operand.isInvalid()) |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 1004 | return move(Operand); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1005 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1006 | return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, move(Operand)); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1007 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1008 | |
| 1009 | static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) |
| 1010 | { |
| 1011 | switch(kind) { |
| 1012 | default: assert(false && "Not a known unary type trait."); |
| 1013 | case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign; |
| 1014 | case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy; |
| 1015 | case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor; |
| 1016 | case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign; |
| 1017 | case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy; |
| 1018 | case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor; |
| 1019 | case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor; |
| 1020 | case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor; |
| 1021 | case tok::kw___is_abstract: return UTT_IsAbstract; |
| 1022 | case tok::kw___is_class: return UTT_IsClass; |
| 1023 | case tok::kw___is_empty: return UTT_IsEmpty; |
| 1024 | case tok::kw___is_enum: return UTT_IsEnum; |
| 1025 | case tok::kw___is_pod: return UTT_IsPOD; |
| 1026 | case tok::kw___is_polymorphic: return UTT_IsPolymorphic; |
| 1027 | case tok::kw___is_union: return UTT_IsUnion; |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | /// ParseUnaryTypeTrait - Parse the built-in unary type-trait |
| 1032 | /// pseudo-functions that allow implementation of the TR1/C++0x type traits |
| 1033 | /// templates. |
| 1034 | /// |
| 1035 | /// primary-expression: |
| 1036 | /// [GNU] unary-type-trait '(' type-id ')' |
| 1037 | /// |
| 1038 | Parser::OwningExprResult Parser::ParseUnaryTypeTrait() |
| 1039 | { |
| 1040 | UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind()); |
| 1041 | SourceLocation Loc = ConsumeToken(); |
| 1042 | |
| 1043 | SourceLocation LParen = Tok.getLocation(); |
| 1044 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen)) |
| 1045 | return ExprError(); |
| 1046 | |
| 1047 | // FIXME: Error reporting absolutely sucks! If the this fails to parse a type |
| 1048 | // there will be cryptic errors about mismatched parentheses and missing |
| 1049 | // specifiers. |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 1050 | TypeResult Ty = ParseTypeName(); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1051 | |
| 1052 | SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen); |
| 1053 | |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 1054 | if (Ty.isInvalid()) |
| 1055 | return ExprError(); |
| 1056 | |
| 1057 | return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty.get(), RParen); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1058 | } |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1059 | |
| 1060 | /// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a |
| 1061 | /// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate |
| 1062 | /// based on the context past the parens. |
| 1063 | Parser::OwningExprResult |
| 1064 | Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType, |
| 1065 | TypeTy *&CastTy, |
| 1066 | SourceLocation LParenLoc, |
| 1067 | SourceLocation &RParenLoc) { |
| 1068 | assert(getLang().CPlusPlus && "Should only be called for C++!"); |
| 1069 | assert(ExprType == CastExpr && "Compound literals are not ambiguous!"); |
| 1070 | assert(isTypeIdInParens() && "Not a type-id!"); |
| 1071 | |
| 1072 | OwningExprResult Result(Actions, true); |
| 1073 | CastTy = 0; |
| 1074 | |
| 1075 | // We need to disambiguate a very ugly part of the C++ syntax: |
| 1076 | // |
| 1077 | // (T())x; - type-id |
| 1078 | // (T())*x; - type-id |
| 1079 | // (T())/x; - expression |
| 1080 | // (T()); - expression |
| 1081 | // |
| 1082 | // The bad news is that we cannot use the specialized tentative parser, since |
| 1083 | // it can only verify that the thing inside the parens can be parsed as |
| 1084 | // type-id, it is not useful for determining the context past the parens. |
| 1085 | // |
| 1086 | // The good news is that the parser can disambiguate this part without |
Argyrios Kyrtzidis | a558a89 | 2009-05-22 15:12:46 +0000 | [diff] [blame] | 1087 | // making any unnecessary Action calls. |
Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 1088 | // |
| 1089 | // It uses a scheme similar to parsing inline methods. The parenthesized |
| 1090 | // tokens are cached, the context that follows is determined (possibly by |
| 1091 | // parsing a cast-expression), and then we re-introduce the cached tokens |
| 1092 | // into the token stream and parse them appropriately. |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1093 | |
Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 1094 | ParenParseOption ParseAs; |
| 1095 | CachedTokens Toks; |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1096 | |
Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 1097 | // Store the tokens of the parentheses. We will parse them after we determine |
| 1098 | // the context that follows them. |
| 1099 | if (!ConsumeAndStoreUntil(tok::r_paren, tok::unknown, Toks, tok::semi)) { |
| 1100 | // We didn't find the ')' we expected. |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1101 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 1102 | return ExprError(); |
| 1103 | } |
| 1104 | |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1105 | if (Tok.is(tok::l_brace)) { |
Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 1106 | ParseAs = CompoundLiteral; |
| 1107 | } else { |
| 1108 | bool NotCastExpr; |
Eli Friedman | b53f08a | 2009-05-25 19:41:42 +0000 | [diff] [blame] | 1109 | // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression |
| 1110 | if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) { |
| 1111 | NotCastExpr = true; |
| 1112 | } else { |
| 1113 | // Try parsing the cast-expression that may follow. |
| 1114 | // If it is not a cast-expression, NotCastExpr will be true and no token |
| 1115 | // will be consumed. |
| 1116 | Result = ParseCastExpression(false/*isUnaryExpression*/, |
| 1117 | false/*isAddressofOperand*/, |
| 1118 | NotCastExpr); |
| 1119 | } |
Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 1120 | |
| 1121 | // If we parsed a cast-expression, it's really a type-id, otherwise it's |
| 1122 | // an expression. |
| 1123 | ParseAs = NotCastExpr ? SimpleExpr : CastExpr; |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1124 | } |
| 1125 | |
Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 1126 | // The current token should go after the cached tokens. |
| 1127 | Toks.push_back(Tok); |
| 1128 | // Re-enter the stored parenthesized tokens into the token stream, so we may |
| 1129 | // parse them now. |
| 1130 | PP.EnterTokenStream(Toks.data(), Toks.size(), |
| 1131 | true/*DisableMacroExpansion*/, false/*OwnsTokens*/); |
| 1132 | // Drop the current token and bring the first cached one. It's the same token |
| 1133 | // as when we entered this function. |
| 1134 | ConsumeAnyToken(); |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1135 | |
Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 1136 | if (ParseAs >= CompoundLiteral) { |
| 1137 | TypeResult Ty = ParseTypeName(); |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1138 | |
Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 1139 | // Match the ')'. |
| 1140 | if (Tok.is(tok::r_paren)) |
| 1141 | RParenLoc = ConsumeParen(); |
| 1142 | else |
| 1143 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1144 | |
Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 1145 | if (ParseAs == CompoundLiteral) { |
| 1146 | ExprType = CompoundLiteral; |
| 1147 | return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc); |
| 1148 | } |
| 1149 | |
| 1150 | // We parsed '(' type-id ')' and the thing after it wasn't a '{'. |
| 1151 | assert(ParseAs == CastExpr); |
| 1152 | |
| 1153 | if (Ty.isInvalid()) |
| 1154 | return ExprError(); |
| 1155 | |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1156 | CastTy = Ty.get(); |
Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 1157 | |
| 1158 | // Result is what ParseCastExpression returned earlier. |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1159 | if (!Result.isInvalid()) |
| 1160 | Result = Actions.ActOnCastExpr(LParenLoc, CastTy, RParenLoc,move(Result)); |
| 1161 | return move(Result); |
| 1162 | } |
Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 1163 | |
| 1164 | // Not a compound literal, and not followed by a cast-expression. |
| 1165 | assert(ParseAs == SimpleExpr); |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1166 | |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1167 | ExprType = SimpleExpr; |
Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 1168 | Result = ParseExpression(); |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1169 | if (!Result.isInvalid() && Tok.is(tok::r_paren)) |
| 1170 | Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), move(Result)); |
| 1171 | |
| 1172 | // Match the ')'. |
| 1173 | if (Result.isInvalid()) { |
| 1174 | SkipUntil(tok::r_paren); |
| 1175 | return ExprError(); |
| 1176 | } |
Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 1177 | |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1178 | if (Tok.is(tok::r_paren)) |
| 1179 | RParenLoc = ConsumeParen(); |
| 1180 | else |
| 1181 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 1182 | |
| 1183 | return move(Result); |
| 1184 | } |