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 | |
| 14 | #include "clang/Basic/Diagnostic.h" |
| 15 | #include "clang/Parse/Parser.h" |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 16 | #include "clang/Parse/DeclSpec.h" |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 17 | #include "AstGuard.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | using namespace clang; |
| 19 | |
Argyrios Kyrtzidis | 4bdd91c | 2008-11-26 21:41:52 +0000 | [diff] [blame] | 20 | /// MaybeParseCXXScopeSpecifier - Parse global scope or nested-name-specifier. |
| 21 | /// Returns true if a nested-name-specifier was parsed from the token stream. |
Chris Lattner | 55a7cef | 2009-01-05 00:13:00 +0000 | [diff] [blame] | 22 | /// |
| 23 | /// Note that this routine emits an error if you call it with ::new or ::delete |
| 24 | /// as the current tokens, so only call it in contexts where these are invalid. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 25 | /// |
| 26 | /// '::'[opt] nested-name-specifier |
| 27 | /// '::' |
| 28 | /// |
| 29 | /// nested-name-specifier: |
| 30 | /// type-name '::' |
| 31 | /// namespace-name '::' |
| 32 | /// nested-name-specifier identifier '::' |
| 33 | /// nested-name-specifier 'template'[opt] simple-template-id '::' [TODO] |
| 34 | /// |
Chris Lattner | a7bc7c8 | 2009-01-04 23:23:14 +0000 | [diff] [blame] | 35 | bool Parser::MaybeParseCXXScopeSpecifier(CXXScopeSpec &SS, |
| 36 | const Token *GlobalQualifier) { |
Argyrios Kyrtzidis | 4bdd91c | 2008-11-26 21:41:52 +0000 | [diff] [blame] | 37 | assert(getLang().CPlusPlus && |
Chris Lattner | 7452c6f | 2009-01-05 01:24:05 +0000 | [diff] [blame] | 38 | "Call sites of this function should be guarded by checking for C++"); |
Argyrios Kyrtzidis | 4bdd91c | 2008-11-26 21:41:52 +0000 | [diff] [blame] | 39 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 40 | if (Tok.is(tok::annot_cxxscope)) { |
Chris Lattner | a7bc7c8 | 2009-01-04 23:23:14 +0000 | [diff] [blame] | 41 | assert(GlobalQualifier == 0 && |
Chris Lattner | 55a7cef | 2009-01-05 00:13:00 +0000 | [diff] [blame] | 42 | "Cannot have :: followed by a resolved annotation scope"); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 43 | SS.setScopeRep(Tok.getAnnotationValue()); |
| 44 | SS.setRange(Tok.getAnnotationRange()); |
| 45 | ConsumeToken(); |
Argyrios Kyrtzidis | 4bdd91c | 2008-11-26 21:41:52 +0000 | [diff] [blame] | 46 | return true; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 47 | } |
Chris Lattner | e607e80 | 2009-01-04 21:14:15 +0000 | [diff] [blame] | 48 | |
Chris Lattner | a7bc7c8 | 2009-01-04 23:23:14 +0000 | [diff] [blame] | 49 | if (GlobalQualifier == 0 && |
| 50 | Tok.isNot(tok::coloncolon) && |
Chris Lattner | e607e80 | 2009-01-04 21:14:15 +0000 | [diff] [blame] | 51 | (Tok.isNot(tok::identifier) || NextToken().isNot(tok::coloncolon))) |
| 52 | return false; |
| 53 | |
Chris Lattner | a7bc7c8 | 2009-01-04 23:23:14 +0000 | [diff] [blame] | 54 | if (GlobalQualifier) { |
| 55 | // Pre-parsed '::'. |
| 56 | SS.setBeginLoc(GlobalQualifier->getLocation()); |
| 57 | SS.setScopeRep(Actions.ActOnCXXGlobalScopeSpecifier(CurScope, |
| 58 | GlobalQualifier->getLocation())); |
| 59 | SS.setEndLoc(GlobalQualifier->getLocation()); |
Chris Lattner | 55a7cef | 2009-01-05 00:13:00 +0000 | [diff] [blame] | 60 | |
| 61 | assert(Tok.isNot(tok::kw_new) && Tok.isNot(tok::kw_delete) && |
| 62 | "Never called with preparsed :: qualifier and with new/delete"); |
Chris Lattner | a7bc7c8 | 2009-01-04 23:23:14 +0000 | [diff] [blame] | 63 | } else { |
| 64 | SS.setBeginLoc(Tok.getLocation()); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 65 | |
Chris Lattner | 55a7cef | 2009-01-05 00:13:00 +0000 | [diff] [blame] | 66 | // '::' - Global scope qualifier. |
Chris Lattner | a7bc7c8 | 2009-01-04 23:23:14 +0000 | [diff] [blame] | 67 | if (Tok.is(tok::coloncolon)) { |
Chris Lattner | a7bc7c8 | 2009-01-04 23:23:14 +0000 | [diff] [blame] | 68 | SourceLocation CCLoc = ConsumeToken(); |
Chris Lattner | 55a7cef | 2009-01-05 00:13:00 +0000 | [diff] [blame] | 69 | |
| 70 | // ::new and ::delete aren't nested-name-specifiers, and |
| 71 | // MaybeParseCXXScopeSpecifier is never called in a context where one could |
| 72 | // exist. This means that if we see it, we have a syntax error. |
| 73 | if (Tok.is(tok::kw_new) || Tok.is(tok::kw_delete)) { |
| 74 | Diag(Tok, diag::err_invalid_qualified_new_delete) |
| 75 | << Tok.is(tok::kw_delete); |
| 76 | SS.setBeginLoc(SourceLocation()); |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | // Global scope. |
Chris Lattner | a7bc7c8 | 2009-01-04 23:23:14 +0000 | [diff] [blame] | 81 | SS.setScopeRep(Actions.ActOnCXXGlobalScopeSpecifier(CurScope, CCLoc)); |
| 82 | SS.setEndLoc(CCLoc); |
| 83 | } |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | // nested-name-specifier: |
| 87 | // type-name '::' |
| 88 | // namespace-name '::' |
| 89 | // nested-name-specifier identifier '::' |
| 90 | // nested-name-specifier 'template'[opt] simple-template-id '::' [TODO] |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 91 | while (Tok.is(tok::identifier) && NextToken().is(tok::coloncolon)) { |
| 92 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 93 | SourceLocation IdLoc = ConsumeToken(); |
Chris Lattner | e607e80 | 2009-01-04 21:14:15 +0000 | [diff] [blame] | 94 | assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!"); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 95 | SourceLocation CCLoc = ConsumeToken(); |
| 96 | if (SS.isInvalid()) |
| 97 | continue; |
| 98 | |
| 99 | SS.setScopeRep( |
Chris Lattner | e607e80 | 2009-01-04 21:14:15 +0000 | [diff] [blame] | 100 | Actions.ActOnCXXNestedNameSpecifier(CurScope, SS, IdLoc, CCLoc, *II)); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 101 | SS.setEndLoc(CCLoc); |
| 102 | } |
Argyrios Kyrtzidis | 4bdd91c | 2008-11-26 21:41:52 +0000 | [diff] [blame] | 103 | |
| 104 | return true; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /// ParseCXXIdExpression - Handle id-expression. |
| 108 | /// |
| 109 | /// id-expression: |
| 110 | /// unqualified-id |
| 111 | /// qualified-id |
| 112 | /// |
| 113 | /// unqualified-id: |
| 114 | /// identifier |
| 115 | /// operator-function-id |
| 116 | /// conversion-function-id [TODO] |
| 117 | /// '~' class-name [TODO] |
| 118 | /// template-id [TODO] |
| 119 | /// |
| 120 | /// qualified-id: |
| 121 | /// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id |
| 122 | /// '::' identifier |
| 123 | /// '::' operator-function-id |
| 124 | /// '::' template-id [TODO] |
| 125 | /// |
| 126 | /// nested-name-specifier: |
| 127 | /// type-name '::' |
| 128 | /// namespace-name '::' |
| 129 | /// nested-name-specifier identifier '::' |
| 130 | /// nested-name-specifier 'template'[opt] simple-template-id '::' [TODO] |
| 131 | /// |
| 132 | /// NOTE: The standard specifies that, for qualified-id, the parser does not |
| 133 | /// expect: |
| 134 | /// |
| 135 | /// '::' conversion-function-id |
| 136 | /// '::' '~' class-name |
| 137 | /// |
| 138 | /// This may cause a slight inconsistency on diagnostics: |
| 139 | /// |
| 140 | /// class C {}; |
| 141 | /// namespace A {} |
| 142 | /// void f() { |
| 143 | /// :: A :: ~ C(); // Some Sema error about using destructor with a |
| 144 | /// // namespace. |
| 145 | /// :: ~ C(); // Some Parser error like 'unexpected ~'. |
| 146 | /// } |
| 147 | /// |
| 148 | /// We simplify the parser a bit and make it work like: |
| 149 | /// |
| 150 | /// qualified-id: |
| 151 | /// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id |
| 152 | /// '::' unqualified-id |
| 153 | /// |
| 154 | /// That way Sema can handle and report similar errors for namespaces and the |
| 155 | /// global scope. |
| 156 | /// |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 157 | Parser::OwningExprResult Parser::ParseCXXIdExpression() { |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 158 | // qualified-id: |
| 159 | // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id |
| 160 | // '::' unqualified-id |
| 161 | // |
| 162 | CXXScopeSpec SS; |
Argyrios Kyrtzidis | 4bdd91c | 2008-11-26 21:41:52 +0000 | [diff] [blame] | 163 | MaybeParseCXXScopeSpecifier(SS); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 164 | |
| 165 | // unqualified-id: |
| 166 | // identifier |
| 167 | // operator-function-id |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 168 | // conversion-function-id |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 169 | // '~' class-name [TODO] |
| 170 | // template-id [TODO] |
| 171 | // |
| 172 | switch (Tok.getKind()) { |
| 173 | default: |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 174 | return ExprError(Diag(Tok, diag::err_expected_unqualified_id)); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 175 | |
| 176 | case tok::identifier: { |
| 177 | // Consume the identifier so that we can see if it is followed by a '('. |
| 178 | IdentifierInfo &II = *Tok.getIdentifierInfo(); |
| 179 | SourceLocation L = ConsumeToken(); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 180 | return Owned(Actions.ActOnIdentifierExpr(CurScope, L, II, |
| 181 | Tok.is(tok::l_paren), &SS)); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | case tok::kw_operator: { |
| 185 | SourceLocation OperatorLoc = Tok.getLocation(); |
Chris Lattner | 7452c6f | 2009-01-05 01:24:05 +0000 | [diff] [blame] | 186 | if (OverloadedOperatorKind Op = TryParseOperatorFunctionId()) |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 187 | return Owned(Actions.ActOnCXXOperatorFunctionIdExpr( |
| 188 | CurScope, OperatorLoc, Op, Tok.is(tok::l_paren), SS)); |
Chris Lattner | 7452c6f | 2009-01-05 01:24:05 +0000 | [diff] [blame] | 189 | if (TypeTy *Type = ParseConversionFunctionId()) |
| 190 | return Owned(Actions.ActOnCXXConversionFunctionExpr(CurScope, OperatorLoc, |
| 191 | Type, |
| 192 | Tok.is(tok::l_paren), SS)); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 193 | |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 194 | // We already complained about a bad conversion-function-id, |
| 195 | // above. |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 196 | return ExprError(); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | } // switch. |
| 200 | |
| 201 | assert(0 && "The switch was supposed to take care everything."); |
| 202 | } |
| 203 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 204 | /// ParseCXXCasts - This handles the various ways to cast expressions to another |
| 205 | /// type. |
| 206 | /// |
| 207 | /// postfix-expression: [C++ 5.2p1] |
| 208 | /// 'dynamic_cast' '<' type-name '>' '(' expression ')' |
| 209 | /// 'static_cast' '<' type-name '>' '(' expression ')' |
| 210 | /// 'reinterpret_cast' '<' type-name '>' '(' expression ')' |
| 211 | /// 'const_cast' '<' type-name '>' '(' expression ')' |
| 212 | /// |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 213 | Parser::OwningExprResult Parser::ParseCXXCasts() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 214 | tok::TokenKind Kind = Tok.getKind(); |
| 215 | const char *CastName = 0; // For error messages |
| 216 | |
| 217 | switch (Kind) { |
| 218 | default: assert(0 && "Unknown C++ cast!"); abort(); |
| 219 | case tok::kw_const_cast: CastName = "const_cast"; break; |
| 220 | case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break; |
| 221 | case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break; |
| 222 | case tok::kw_static_cast: CastName = "static_cast"; break; |
| 223 | } |
| 224 | |
| 225 | SourceLocation OpLoc = ConsumeToken(); |
| 226 | SourceLocation LAngleBracketLoc = Tok.getLocation(); |
| 227 | |
| 228 | if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName)) |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 229 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 230 | |
| 231 | TypeTy *CastTy = ParseTypeName(); |
| 232 | SourceLocation RAngleBracketLoc = Tok.getLocation(); |
| 233 | |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 234 | if (ExpectAndConsume(tok::greater, diag::err_expected_greater)) |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 235 | return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 236 | |
| 237 | SourceLocation LParenLoc = Tok.getLocation(), RParenLoc; |
| 238 | |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 239 | if (Tok.isNot(tok::l_paren)) |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 240 | return ExprError(Diag(Tok, diag::err_expected_lparen_after) << CastName); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 241 | |
Sebastian Redl | d8c4e15 | 2008-12-11 22:33:27 +0000 | [diff] [blame] | 242 | OwningExprResult Result(ParseSimpleParenExpression(RParenLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 243 | |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 244 | if (!Result.isInvalid()) |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 245 | Result = Actions.ActOnCXXNamedCast(OpLoc, Kind, |
| 246 | LAngleBracketLoc, CastTy, RAngleBracketLoc, |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 247 | LParenLoc, Result.release(), RParenLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 248 | |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 249 | return move(Result); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 252 | /// ParseCXXTypeid - This handles the C++ typeid expression. |
| 253 | /// |
| 254 | /// postfix-expression: [C++ 5.2p1] |
| 255 | /// 'typeid' '(' expression ')' |
| 256 | /// 'typeid' '(' type-id ')' |
| 257 | /// |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 258 | Parser::OwningExprResult Parser::ParseCXXTypeid() { |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 259 | assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!"); |
| 260 | |
| 261 | SourceLocation OpLoc = ConsumeToken(); |
| 262 | SourceLocation LParenLoc = Tok.getLocation(); |
| 263 | SourceLocation RParenLoc; |
| 264 | |
| 265 | // typeid expressions are always parenthesized. |
| 266 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 267 | "typeid")) |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 268 | return ExprError(); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 269 | |
Sebastian Redl | 15faa7f | 2008-12-09 20:22:58 +0000 | [diff] [blame] | 270 | OwningExprResult Result(Actions); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 271 | |
| 272 | if (isTypeIdInParens()) { |
| 273 | TypeTy *Ty = ParseTypeName(); |
| 274 | |
| 275 | // Match the ')'. |
| 276 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 277 | |
| 278 | if (!Ty) |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 279 | return ExprError(); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 280 | |
| 281 | Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true, |
| 282 | Ty, RParenLoc); |
| 283 | } else { |
| 284 | Result = ParseExpression(); |
| 285 | |
| 286 | // Match the ')'. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 287 | if (Result.isInvalid()) |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 288 | SkipUntil(tok::r_paren); |
| 289 | else { |
| 290 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 291 | |
| 292 | Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false, |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 293 | Result.release(), RParenLoc); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 297 | return move(Result); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 300 | /// ParseCXXBoolLiteral - This handles the C++ Boolean literals. |
| 301 | /// |
| 302 | /// boolean-literal: [C++ 2.13.5] |
| 303 | /// 'true' |
| 304 | /// 'false' |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 305 | Parser::OwningExprResult Parser::ParseCXXBoolLiteral() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 306 | tok::TokenKind Kind = Tok.getKind(); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 307 | return Owned(Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 308 | } |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 309 | |
| 310 | /// ParseThrowExpression - This handles the C++ throw expression. |
| 311 | /// |
| 312 | /// throw-expression: [C++ 15] |
| 313 | /// 'throw' assignment-expression[opt] |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 314 | Parser::OwningExprResult Parser::ParseThrowExpression() { |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 315 | assert(Tok.is(tok::kw_throw) && "Not throw!"); |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 316 | SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token. |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 317 | |
Chris Lattner | 2a2819a | 2008-04-06 06:02:23 +0000 | [diff] [blame] | 318 | // If the current token isn't the start of an assignment-expression, |
| 319 | // then the expression is not present. This handles things like: |
| 320 | // "C ? throw : (void)42", which is crazy but legal. |
| 321 | switch (Tok.getKind()) { // FIXME: move this predicate somewhere common. |
| 322 | case tok::semi: |
| 323 | case tok::r_paren: |
| 324 | case tok::r_square: |
| 325 | case tok::r_brace: |
| 326 | case tok::colon: |
| 327 | case tok::comma: |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 328 | return Owned(Actions.ActOnCXXThrow(ThrowLoc)); |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 329 | |
Chris Lattner | 2a2819a | 2008-04-06 06:02:23 +0000 | [diff] [blame] | 330 | default: |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 331 | OwningExprResult Expr(ParseAssignmentExpression()); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 332 | if (Expr.isInvalid()) return move(Expr); |
| 333 | return Owned(Actions.ActOnCXXThrow(ThrowLoc, Expr.release())); |
Chris Lattner | 2a2819a | 2008-04-06 06:02:23 +0000 | [diff] [blame] | 334 | } |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 335 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 336 | |
| 337 | /// ParseCXXThis - This handles the C++ 'this' pointer. |
| 338 | /// |
| 339 | /// C++ 9.3.2: In the body of a non-static member function, the keyword this is |
| 340 | /// a non-lvalue expression whose value is the address of the object for which |
| 341 | /// the function is called. |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 342 | Parser::OwningExprResult Parser::ParseCXXThis() { |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 343 | assert(Tok.is(tok::kw_this) && "Not 'this'!"); |
| 344 | SourceLocation ThisLoc = ConsumeToken(); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 345 | return Owned(Actions.ActOnCXXThis(ThisLoc)); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 346 | } |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 347 | |
| 348 | /// ParseCXXTypeConstructExpression - Parse construction of a specified type. |
| 349 | /// Can be interpreted either as function-style casting ("int(x)") |
| 350 | /// or class type construction ("ClassType(x,y,z)") |
| 351 | /// or creation of a value-initialized type ("int()"). |
| 352 | /// |
| 353 | /// postfix-expression: [C++ 5.2p1] |
| 354 | /// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3] |
| 355 | /// typename-specifier '(' expression-list[opt] ')' [TODO] |
| 356 | /// |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 357 | Parser::OwningExprResult |
| 358 | Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) { |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 359 | Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); |
| 360 | TypeTy *TypeRep = Actions.ActOnTypeName(CurScope, DeclaratorInfo).Val; |
| 361 | |
| 362 | assert(Tok.is(tok::l_paren) && "Expected '('!"); |
| 363 | SourceLocation LParenLoc = ConsumeParen(); |
| 364 | |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 365 | ExprVector Exprs(Actions); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 366 | CommaLocsTy CommaLocs; |
| 367 | |
| 368 | if (Tok.isNot(tok::r_paren)) { |
| 369 | if (ParseExpressionList(Exprs, CommaLocs)) { |
| 370 | SkipUntil(tok::r_paren); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 371 | return ExprError(); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | |
| 375 | // Match the ')'. |
| 376 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 377 | |
| 378 | assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&& |
| 379 | "Unexpected number of commas!"); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 380 | return Owned(Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep, |
| 381 | LParenLoc, |
| 382 | Exprs.take(), Exprs.size(), |
| 383 | &CommaLocs[0], RParenLoc)); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 386 | /// ParseCXXCondition - if/switch/while/for condition expression. |
| 387 | /// |
| 388 | /// condition: |
| 389 | /// expression |
| 390 | /// type-specifier-seq declarator '=' assignment-expression |
| 391 | /// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt] |
| 392 | /// '=' assignment-expression |
| 393 | /// |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 394 | Parser::OwningExprResult Parser::ParseCXXCondition() { |
Argyrios Kyrtzidis | a8a4598 | 2008-10-05 15:03:47 +0000 | [diff] [blame] | 395 | if (!isCXXConditionDeclaration()) |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 396 | return ParseExpression(); // expression |
| 397 | |
| 398 | SourceLocation StartLoc = Tok.getLocation(); |
| 399 | |
| 400 | // type-specifier-seq |
| 401 | DeclSpec DS; |
| 402 | ParseSpecifierQualifierList(DS); |
| 403 | |
| 404 | // declarator |
| 405 | Declarator DeclaratorInfo(DS, Declarator::ConditionContext); |
| 406 | ParseDeclarator(DeclaratorInfo); |
| 407 | |
| 408 | // simple-asm-expr[opt] |
| 409 | if (Tok.is(tok::kw_asm)) { |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 410 | OwningExprResult AsmLabel(ParseSimpleAsm()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 411 | if (AsmLabel.isInvalid()) { |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 412 | SkipUntil(tok::semi); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 413 | return ExprError(); |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 414 | } |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 415 | DeclaratorInfo.setAsmLabel(AsmLabel.release()); |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | // If attributes are present, parse them. |
| 419 | if (Tok.is(tok::kw___attribute)) |
| 420 | DeclaratorInfo.AddAttributes(ParseAttributes()); |
| 421 | |
| 422 | // '=' assignment-expression |
| 423 | if (Tok.isNot(tok::equal)) |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 424 | return ExprError(Diag(Tok, diag::err_expected_equal_after_declarator)); |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 425 | SourceLocation EqualLoc = ConsumeToken(); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 426 | OwningExprResult AssignExpr(ParseAssignmentExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 427 | if (AssignExpr.isInvalid()) |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 428 | return ExprError(); |
| 429 | |
| 430 | return Owned(Actions.ActOnCXXConditionDeclarationExpr(CurScope, StartLoc, |
| 431 | DeclaratorInfo,EqualLoc, |
| 432 | AssignExpr.release())); |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 435 | /// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers. |
| 436 | /// This should only be called when the current token is known to be part of |
| 437 | /// simple-type-specifier. |
| 438 | /// |
| 439 | /// simple-type-specifier: |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 440 | /// '::'[opt] nested-name-specifier[opt] type-name |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 441 | /// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO] |
| 442 | /// char |
| 443 | /// wchar_t |
| 444 | /// bool |
| 445 | /// short |
| 446 | /// int |
| 447 | /// long |
| 448 | /// signed |
| 449 | /// unsigned |
| 450 | /// float |
| 451 | /// double |
| 452 | /// void |
| 453 | /// [GNU] typeof-specifier |
| 454 | /// [C++0x] auto [TODO] |
| 455 | /// |
| 456 | /// type-name: |
| 457 | /// class-name |
| 458 | /// enum-name |
| 459 | /// typedef-name |
| 460 | /// |
| 461 | void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) { |
| 462 | DS.SetRangeStart(Tok.getLocation()); |
| 463 | const char *PrevSpec; |
| 464 | SourceLocation Loc = Tok.getLocation(); |
| 465 | |
| 466 | switch (Tok.getKind()) { |
Chris Lattner | 55a7cef | 2009-01-05 00:13:00 +0000 | [diff] [blame] | 467 | case tok::identifier: // foo::bar |
| 468 | case tok::coloncolon: // ::foo::bar |
| 469 | assert(0 && "Annotation token should already be formed!"); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 470 | default: |
| 471 | assert(0 && "Not a simple-type-specifier token!"); |
| 472 | abort(); |
Chris Lattner | 55a7cef | 2009-01-05 00:13:00 +0000 | [diff] [blame] | 473 | |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 474 | // type-name |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 475 | case tok::annot_qualtypename: { |
| 476 | DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec, |
| 477 | Tok.getAnnotationValue()); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 478 | break; |
| 479 | } |
| 480 | |
| 481 | // builtin types |
| 482 | case tok::kw_short: |
| 483 | DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec); |
| 484 | break; |
| 485 | case tok::kw_long: |
| 486 | DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec); |
| 487 | break; |
| 488 | case tok::kw_signed: |
| 489 | DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec); |
| 490 | break; |
| 491 | case tok::kw_unsigned: |
| 492 | DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec); |
| 493 | break; |
| 494 | case tok::kw_void: |
| 495 | DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec); |
| 496 | break; |
| 497 | case tok::kw_char: |
| 498 | DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec); |
| 499 | break; |
| 500 | case tok::kw_int: |
| 501 | DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec); |
| 502 | break; |
| 503 | case tok::kw_float: |
| 504 | DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec); |
| 505 | break; |
| 506 | case tok::kw_double: |
| 507 | DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec); |
| 508 | break; |
| 509 | case tok::kw_wchar_t: |
| 510 | DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec); |
| 511 | break; |
| 512 | case tok::kw_bool: |
| 513 | DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec); |
| 514 | break; |
| 515 | |
| 516 | // GNU typeof support. |
| 517 | case tok::kw_typeof: |
| 518 | ParseTypeofSpecifier(DS); |
| 519 | DS.Finish(Diags, PP.getSourceManager(), getLang()); |
| 520 | return; |
| 521 | } |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 522 | if (Tok.is(tok::annot_qualtypename)) |
| 523 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 524 | else |
| 525 | DS.SetRangeEnd(Tok.getLocation()); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 526 | ConsumeToken(); |
| 527 | DS.Finish(Diags, PP.getSourceManager(), getLang()); |
| 528 | } |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 529 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 530 | /// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++ |
| 531 | /// [dcl.name]), which is a non-empty sequence of type-specifiers, |
| 532 | /// e.g., "const short int". Note that the DeclSpec is *not* finished |
| 533 | /// by parsing the type-specifier-seq, because these sequences are |
| 534 | /// typically followed by some form of declarator. Returns true and |
| 535 | /// emits diagnostics if this is not a type-specifier-seq, false |
| 536 | /// otherwise. |
| 537 | /// |
| 538 | /// type-specifier-seq: [C++ 8.1] |
| 539 | /// type-specifier type-specifier-seq[opt] |
| 540 | /// |
| 541 | bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) { |
| 542 | DS.SetRangeStart(Tok.getLocation()); |
| 543 | const char *PrevSpec = 0; |
| 544 | int isInvalid = 0; |
| 545 | |
| 546 | // Parse one or more of the type specifiers. |
| 547 | if (!MaybeParseTypeSpecifier(DS, isInvalid, PrevSpec)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 548 | Diag(Tok, diag::err_operator_missing_type_specifier); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 549 | return true; |
| 550 | } |
Daniel Dunbar | b90585c | 2008-11-08 04:28:37 +0000 | [diff] [blame] | 551 | while (MaybeParseTypeSpecifier(DS, isInvalid, PrevSpec)) ; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 552 | |
| 553 | return false; |
| 554 | } |
| 555 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 556 | /// TryParseOperatorFunctionId - Attempts to parse a C++ overloaded |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 557 | /// operator name (C++ [over.oper]). If successful, returns the |
| 558 | /// predefined identifier that corresponds to that overloaded |
| 559 | /// operator. Otherwise, returns NULL and does not consume any tokens. |
| 560 | /// |
| 561 | /// operator-function-id: [C++ 13.5] |
| 562 | /// 'operator' operator |
| 563 | /// |
| 564 | /// operator: one of |
| 565 | /// new delete new[] delete[] |
| 566 | /// + - * / % ^ & | ~ |
| 567 | /// ! = < > += -= *= /= %= |
| 568 | /// ^= &= |= << >> >>= <<= == != |
| 569 | /// <= >= && || ++ -- , ->* -> |
| 570 | /// () [] |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 571 | OverloadedOperatorKind Parser::TryParseOperatorFunctionId() { |
Argyrios Kyrtzidis | 9057a81 | 2008-11-07 15:54:02 +0000 | [diff] [blame] | 572 | assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword"); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 573 | |
| 574 | OverloadedOperatorKind Op = OO_None; |
| 575 | switch (NextToken().getKind()) { |
| 576 | case tok::kw_new: |
| 577 | ConsumeToken(); // 'operator' |
| 578 | ConsumeToken(); // 'new' |
| 579 | if (Tok.is(tok::l_square)) { |
| 580 | ConsumeBracket(); // '[' |
| 581 | ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']' |
| 582 | Op = OO_Array_New; |
| 583 | } else { |
| 584 | Op = OO_New; |
| 585 | } |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 586 | return Op; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 587 | |
| 588 | case tok::kw_delete: |
| 589 | ConsumeToken(); // 'operator' |
| 590 | ConsumeToken(); // 'delete' |
| 591 | if (Tok.is(tok::l_square)) { |
| 592 | ConsumeBracket(); // '[' |
| 593 | ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']' |
| 594 | Op = OO_Array_Delete; |
| 595 | } else { |
| 596 | Op = OO_Delete; |
| 597 | } |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 598 | return Op; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 599 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 600 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 601 | case tok::Token: Op = OO_##Name; break; |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 602 | #define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly) |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 603 | #include "clang/Basic/OperatorKinds.def" |
| 604 | |
| 605 | case tok::l_paren: |
| 606 | ConsumeToken(); // 'operator' |
| 607 | ConsumeParen(); // '(' |
| 608 | ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); // ')' |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 609 | return OO_Call; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 610 | |
| 611 | case tok::l_square: |
| 612 | ConsumeToken(); // 'operator' |
| 613 | ConsumeBracket(); // '[' |
| 614 | ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']' |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 615 | return OO_Subscript; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 616 | |
| 617 | default: |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 618 | return OO_None; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 621 | ConsumeToken(); // 'operator' |
| 622 | ConsumeAnyToken(); // the operator itself |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 623 | return Op; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 624 | } |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 625 | |
| 626 | /// ParseConversionFunctionId - Parse a C++ conversion-function-id, |
| 627 | /// which expresses the name of a user-defined conversion operator |
| 628 | /// (C++ [class.conv.fct]p1). Returns the type that this operator is |
| 629 | /// specifying a conversion for, or NULL if there was an error. |
| 630 | /// |
| 631 | /// conversion-function-id: [C++ 12.3.2] |
| 632 | /// operator conversion-type-id |
| 633 | /// |
| 634 | /// conversion-type-id: |
| 635 | /// type-specifier-seq conversion-declarator[opt] |
| 636 | /// |
| 637 | /// conversion-declarator: |
| 638 | /// ptr-operator conversion-declarator[opt] |
| 639 | Parser::TypeTy *Parser::ParseConversionFunctionId() { |
| 640 | assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword"); |
| 641 | ConsumeToken(); // 'operator' |
| 642 | |
| 643 | // Parse the type-specifier-seq. |
| 644 | DeclSpec DS; |
| 645 | if (ParseCXXTypeSpecifierSeq(DS)) |
| 646 | return 0; |
| 647 | |
| 648 | // Parse the conversion-declarator, which is merely a sequence of |
| 649 | // ptr-operators. |
| 650 | Declarator D(DS, Declarator::TypeNameContext); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 651 | ParseDeclaratorInternal(D, /*DirectDeclParser=*/0); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 652 | |
| 653 | // Finish up the type. |
| 654 | Action::TypeResult Result = Actions.ActOnTypeName(CurScope, D); |
| 655 | if (Result.isInvalid) |
| 656 | return 0; |
| 657 | else |
| 658 | return Result.Val; |
| 659 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 660 | |
| 661 | /// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate |
| 662 | /// memory in a typesafe manner and call constructors. |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 663 | /// |
| 664 | /// This method is called to parse the new expression after the optional :: has |
| 665 | /// been already parsed. If the :: was present, "UseGlobal" is true and "Start" |
| 666 | /// is its location. Otherwise, "Start" is the location of the 'new' token. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 667 | /// |
| 668 | /// new-expression: |
| 669 | /// '::'[opt] 'new' new-placement[opt] new-type-id |
| 670 | /// new-initializer[opt] |
| 671 | /// '::'[opt] 'new' new-placement[opt] '(' type-id ')' |
| 672 | /// new-initializer[opt] |
| 673 | /// |
| 674 | /// new-placement: |
| 675 | /// '(' expression-list ')' |
| 676 | /// |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 677 | /// new-type-id: |
| 678 | /// type-specifier-seq new-declarator[opt] |
| 679 | /// |
| 680 | /// new-declarator: |
| 681 | /// ptr-operator new-declarator[opt] |
| 682 | /// direct-new-declarator |
| 683 | /// |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 684 | /// new-initializer: |
| 685 | /// '(' expression-list[opt] ')' |
| 686 | /// [C++0x] braced-init-list [TODO] |
| 687 | /// |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 688 | Parser::OwningExprResult |
| 689 | Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) { |
| 690 | assert(Tok.is(tok::kw_new) && "expected 'new' token"); |
| 691 | ConsumeToken(); // Consume 'new' |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 692 | |
| 693 | // A '(' now can be a new-placement or the '(' wrapping the type-id in the |
| 694 | // second form of new-expression. It can't be a new-type-id. |
| 695 | |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 696 | ExprVector PlacementArgs(Actions); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 697 | SourceLocation PlacementLParen, PlacementRParen; |
| 698 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 699 | bool ParenTypeId; |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 700 | DeclSpec DS; |
| 701 | Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 702 | if (Tok.is(tok::l_paren)) { |
| 703 | // If it turns out to be a placement, we change the type location. |
| 704 | PlacementLParen = ConsumeParen(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 705 | if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) { |
| 706 | SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 707 | return ExprError(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 708 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 709 | |
| 710 | PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 711 | if (PlacementRParen.isInvalid()) { |
| 712 | SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 713 | return ExprError(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 714 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 715 | |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 716 | if (PlacementArgs.empty()) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 717 | // Reset the placement locations. There was no placement. |
| 718 | PlacementLParen = PlacementRParen = SourceLocation(); |
| 719 | ParenTypeId = true; |
| 720 | } else { |
| 721 | // We still need the type. |
| 722 | if (Tok.is(tok::l_paren)) { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 723 | SourceLocation LParen = ConsumeParen(); |
| 724 | ParseSpecifierQualifierList(DS); |
| 725 | ParseDeclarator(DeclaratorInfo); |
| 726 | MatchRHSPunctuation(tok::r_paren, LParen); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 727 | ParenTypeId = true; |
| 728 | } else { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 729 | if (ParseCXXTypeSpecifierSeq(DS)) |
| 730 | DeclaratorInfo.setInvalidType(true); |
| 731 | else |
| 732 | ParseDeclaratorInternal(DeclaratorInfo, |
| 733 | &Parser::ParseDirectNewDeclarator); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 734 | ParenTypeId = false; |
| 735 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 736 | } |
| 737 | } else { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 738 | // A new-type-id is a simplified type-id, where essentially the |
| 739 | // direct-declarator is replaced by a direct-new-declarator. |
| 740 | if (ParseCXXTypeSpecifierSeq(DS)) |
| 741 | DeclaratorInfo.setInvalidType(true); |
| 742 | else |
| 743 | ParseDeclaratorInternal(DeclaratorInfo, |
| 744 | &Parser::ParseDirectNewDeclarator); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 745 | ParenTypeId = false; |
| 746 | } |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 747 | if (DeclaratorInfo.getInvalidType()) { |
| 748 | SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 749 | return ExprError(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 750 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 751 | |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 752 | ExprVector ConstructorArgs(Actions); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 753 | SourceLocation ConstructorLParen, ConstructorRParen; |
| 754 | |
| 755 | if (Tok.is(tok::l_paren)) { |
| 756 | ConstructorLParen = ConsumeParen(); |
| 757 | if (Tok.isNot(tok::r_paren)) { |
| 758 | CommaLocsTy CommaLocs; |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 759 | if (ParseExpressionList(ConstructorArgs, CommaLocs)) { |
| 760 | SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 761 | return ExprError(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 762 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 763 | } |
| 764 | ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 765 | if (ConstructorRParen.isInvalid()) { |
| 766 | SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 767 | return ExprError(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 768 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 769 | } |
| 770 | |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 771 | return Owned(Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen, |
| 772 | PlacementArgs.take(), PlacementArgs.size(), |
| 773 | PlacementRParen, ParenTypeId, DeclaratorInfo, |
| 774 | ConstructorLParen, ConstructorArgs.take(), |
| 775 | ConstructorArgs.size(), ConstructorRParen)); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 776 | } |
| 777 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 778 | /// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be |
| 779 | /// passed to ParseDeclaratorInternal. |
| 780 | /// |
| 781 | /// direct-new-declarator: |
| 782 | /// '[' expression ']' |
| 783 | /// direct-new-declarator '[' constant-expression ']' |
| 784 | /// |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 785 | void Parser::ParseDirectNewDeclarator(Declarator &D) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 786 | // Parse the array dimensions. |
| 787 | bool first = true; |
| 788 | while (Tok.is(tok::l_square)) { |
| 789 | SourceLocation LLoc = ConsumeBracket(); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 790 | OwningExprResult Size(first ? ParseExpression() |
| 791 | : ParseConstantExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 792 | if (Size.isInvalid()) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 793 | // Recover |
| 794 | SkipUntil(tok::r_square); |
| 795 | return; |
| 796 | } |
| 797 | first = false; |
| 798 | |
| 799 | D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false, |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 800 | Size.release(), LLoc)); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 801 | |
| 802 | if (MatchRHSPunctuation(tok::r_square, LLoc).isInvalid()) |
| 803 | return; |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | /// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id. |
| 808 | /// This ambiguity appears in the syntax of the C++ new operator. |
| 809 | /// |
| 810 | /// new-expression: |
| 811 | /// '::'[opt] 'new' new-placement[opt] '(' type-id ')' |
| 812 | /// new-initializer[opt] |
| 813 | /// |
| 814 | /// new-placement: |
| 815 | /// '(' expression-list ')' |
| 816 | /// |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 817 | bool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs, |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 818 | Declarator &D) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 819 | // The '(' was already consumed. |
| 820 | if (isTypeIdInParens()) { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 821 | ParseSpecifierQualifierList(D.getMutableDeclSpec()); |
| 822 | ParseDeclarator(D); |
| 823 | return D.getInvalidType(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | // It's not a type, it has to be an expression list. |
| 827 | // Discard the comma locations - ActOnCXXNew has enough parameters. |
| 828 | CommaLocsTy CommaLocs; |
| 829 | return ParseExpressionList(PlacementArgs, CommaLocs); |
| 830 | } |
| 831 | |
| 832 | /// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used |
| 833 | /// to free memory allocated by new. |
| 834 | /// |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 835 | /// This method is called to parse the 'delete' expression after the optional |
| 836 | /// '::' has been already parsed. If the '::' was present, "UseGlobal" is true |
| 837 | /// and "Start" is its location. Otherwise, "Start" is the location of the |
| 838 | /// 'delete' token. |
| 839 | /// |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 840 | /// delete-expression: |
| 841 | /// '::'[opt] 'delete' cast-expression |
| 842 | /// '::'[opt] 'delete' '[' ']' cast-expression |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 843 | Parser::OwningExprResult |
| 844 | Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) { |
| 845 | assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword"); |
| 846 | ConsumeToken(); // Consume 'delete' |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 847 | |
| 848 | // Array delete? |
| 849 | bool ArrayDelete = false; |
| 850 | if (Tok.is(tok::l_square)) { |
| 851 | ArrayDelete = true; |
| 852 | SourceLocation LHS = ConsumeBracket(); |
| 853 | SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS); |
| 854 | if (RHS.isInvalid()) |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 855 | return ExprError(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 856 | } |
| 857 | |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 858 | OwningExprResult Operand(ParseCastExpression(false)); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 859 | if (Operand.isInvalid()) |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 860 | return move(Operand); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 861 | |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 862 | return Owned(Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, |
| 863 | Operand.release())); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 864 | } |