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