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)) { |
| 39 | SS.setScopeRep(Tok.getAnnotationValue()); |
| 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); |
| 56 | SS.setScopeRep(Actions.ActOnCXXGlobalScopeSpecifier(CurScope, CCLoc)); |
| 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 | |
| 82 | SS.setScopeRep( |
| 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 | |
| 120 | DeclTy *Template = 0; |
| 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)) |
| 152 | SS.setScopeRep(0); |
| 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 | |
| 167 | SS.setScopeRep( |
| 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, |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 331 | LAngleBracketLoc, CastTy.get(), |
| 332 | RAngleBracketLoc, |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 333 | LParenLoc, Result.release(), 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 | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 393 | return Owned(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 | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 414 | return Owned(Actions.ActOnCXXThrow(ThrowLoc)); |
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); |
| 419 | return Owned(Actions.ActOnCXXThrow(ThrowLoc, Expr.release())); |
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 | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 431 | return Owned(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 | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 466 | return Owned(Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep, |
| 467 | LParenLoc, |
| 468 | Exprs.take(), Exprs.size(), |
| 469 | &CommaLocs[0], RParenLoc)); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 472 | /// ParseCXXCondition - if/switch/while/for condition expression. |
| 473 | /// |
| 474 | /// condition: |
| 475 | /// expression |
| 476 | /// type-specifier-seq declarator '=' assignment-expression |
| 477 | /// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt] |
| 478 | /// '=' assignment-expression |
| 479 | /// |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 480 | Parser::OwningExprResult Parser::ParseCXXCondition() { |
Argyrios Kyrtzidis | a8a4598 | 2008-10-05 15:03:47 +0000 | [diff] [blame] | 481 | if (!isCXXConditionDeclaration()) |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 482 | return ParseExpression(); // expression |
| 483 | |
| 484 | SourceLocation StartLoc = Tok.getLocation(); |
| 485 | |
| 486 | // type-specifier-seq |
| 487 | DeclSpec DS; |
| 488 | ParseSpecifierQualifierList(DS); |
| 489 | |
| 490 | // declarator |
| 491 | Declarator DeclaratorInfo(DS, Declarator::ConditionContext); |
| 492 | ParseDeclarator(DeclaratorInfo); |
| 493 | |
| 494 | // simple-asm-expr[opt] |
| 495 | if (Tok.is(tok::kw_asm)) { |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 496 | SourceLocation Loc; |
| 497 | OwningExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 498 | if (AsmLabel.isInvalid()) { |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 499 | SkipUntil(tok::semi); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 500 | return ExprError(); |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 501 | } |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 502 | DeclaratorInfo.setAsmLabel(AsmLabel.release()); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 503 | DeclaratorInfo.SetRangeEnd(Loc); |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | // If attributes are present, parse them. |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 507 | if (Tok.is(tok::kw___attribute)) { |
| 508 | SourceLocation Loc; |
| 509 | AttributeList *AttrList = ParseAttributes(&Loc); |
| 510 | DeclaratorInfo.AddAttributes(AttrList, Loc); |
| 511 | } |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 512 | |
| 513 | // '=' assignment-expression |
| 514 | if (Tok.isNot(tok::equal)) |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 515 | return ExprError(Diag(Tok, diag::err_expected_equal_after_declarator)); |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 516 | SourceLocation EqualLoc = ConsumeToken(); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 517 | OwningExprResult AssignExpr(ParseAssignmentExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 518 | if (AssignExpr.isInvalid()) |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 519 | return ExprError(); |
| 520 | |
| 521 | return Owned(Actions.ActOnCXXConditionDeclarationExpr(CurScope, StartLoc, |
| 522 | DeclaratorInfo,EqualLoc, |
| 523 | AssignExpr.release())); |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 526 | /// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers. |
| 527 | /// This should only be called when the current token is known to be part of |
| 528 | /// simple-type-specifier. |
| 529 | /// |
| 530 | /// simple-type-specifier: |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 531 | /// '::'[opt] nested-name-specifier[opt] type-name |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 532 | /// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO] |
| 533 | /// char |
| 534 | /// wchar_t |
| 535 | /// bool |
| 536 | /// short |
| 537 | /// int |
| 538 | /// long |
| 539 | /// signed |
| 540 | /// unsigned |
| 541 | /// float |
| 542 | /// double |
| 543 | /// void |
| 544 | /// [GNU] typeof-specifier |
| 545 | /// [C++0x] auto [TODO] |
| 546 | /// |
| 547 | /// type-name: |
| 548 | /// class-name |
| 549 | /// enum-name |
| 550 | /// typedef-name |
| 551 | /// |
| 552 | void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) { |
| 553 | DS.SetRangeStart(Tok.getLocation()); |
| 554 | const char *PrevSpec; |
| 555 | SourceLocation Loc = Tok.getLocation(); |
| 556 | |
| 557 | switch (Tok.getKind()) { |
Chris Lattner | 55a7cef | 2009-01-05 00:13:00 +0000 | [diff] [blame] | 558 | case tok::identifier: // foo::bar |
| 559 | case tok::coloncolon: // ::foo::bar |
| 560 | assert(0 && "Annotation token should already be formed!"); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 561 | default: |
| 562 | assert(0 && "Not a simple-type-specifier token!"); |
| 563 | abort(); |
Chris Lattner | 55a7cef | 2009-01-05 00:13:00 +0000 | [diff] [blame] | 564 | |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 565 | // type-name |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 566 | case tok::annot_typename: { |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 567 | DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 568 | Tok.getAnnotationValue()); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 569 | break; |
| 570 | } |
| 571 | |
| 572 | // builtin types |
| 573 | case tok::kw_short: |
| 574 | DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec); |
| 575 | break; |
| 576 | case tok::kw_long: |
| 577 | DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec); |
| 578 | break; |
| 579 | case tok::kw_signed: |
| 580 | DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec); |
| 581 | break; |
| 582 | case tok::kw_unsigned: |
| 583 | DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec); |
| 584 | break; |
| 585 | case tok::kw_void: |
| 586 | DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec); |
| 587 | break; |
| 588 | case tok::kw_char: |
| 589 | DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec); |
| 590 | break; |
| 591 | case tok::kw_int: |
| 592 | DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec); |
| 593 | break; |
| 594 | case tok::kw_float: |
| 595 | DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec); |
| 596 | break; |
| 597 | case tok::kw_double: |
| 598 | DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec); |
| 599 | break; |
| 600 | case tok::kw_wchar_t: |
| 601 | DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec); |
| 602 | break; |
| 603 | case tok::kw_bool: |
| 604 | DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec); |
| 605 | break; |
| 606 | |
| 607 | // GNU typeof support. |
| 608 | case tok::kw_typeof: |
| 609 | ParseTypeofSpecifier(DS); |
| 610 | DS.Finish(Diags, PP.getSourceManager(), getLang()); |
| 611 | return; |
| 612 | } |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 613 | if (Tok.is(tok::annot_typename)) |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 614 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 615 | else |
| 616 | DS.SetRangeEnd(Tok.getLocation()); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 617 | ConsumeToken(); |
| 618 | DS.Finish(Diags, PP.getSourceManager(), getLang()); |
| 619 | } |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 620 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 621 | /// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++ |
| 622 | /// [dcl.name]), which is a non-empty sequence of type-specifiers, |
| 623 | /// e.g., "const short int". Note that the DeclSpec is *not* finished |
| 624 | /// by parsing the type-specifier-seq, because these sequences are |
| 625 | /// typically followed by some form of declarator. Returns true and |
| 626 | /// emits diagnostics if this is not a type-specifier-seq, false |
| 627 | /// otherwise. |
| 628 | /// |
| 629 | /// type-specifier-seq: [C++ 8.1] |
| 630 | /// type-specifier type-specifier-seq[opt] |
| 631 | /// |
| 632 | bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) { |
| 633 | DS.SetRangeStart(Tok.getLocation()); |
| 634 | const char *PrevSpec = 0; |
| 635 | int isInvalid = 0; |
| 636 | |
| 637 | // Parse one or more of the type specifiers. |
Chris Lattner | 7a0ab5f | 2009-01-06 06:59:53 +0000 | [diff] [blame] | 638 | if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 639 | Diag(Tok, diag::err_operator_missing_type_specifier); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 640 | return true; |
| 641 | } |
Chris Lattner | 7a0ab5f | 2009-01-06 06:59:53 +0000 | [diff] [blame] | 642 | |
Ted Kremenek | b8006e5 | 2009-01-06 19:17:58 +0000 | [diff] [blame] | 643 | while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec)) ; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 644 | |
| 645 | return false; |
| 646 | } |
| 647 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 648 | /// TryParseOperatorFunctionId - Attempts to parse a C++ overloaded |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 649 | /// operator name (C++ [over.oper]). If successful, returns the |
| 650 | /// predefined identifier that corresponds to that overloaded |
| 651 | /// operator. Otherwise, returns NULL and does not consume any tokens. |
| 652 | /// |
| 653 | /// operator-function-id: [C++ 13.5] |
| 654 | /// 'operator' operator |
| 655 | /// |
| 656 | /// operator: one of |
| 657 | /// new delete new[] delete[] |
| 658 | /// + - * / % ^ & | ~ |
| 659 | /// ! = < > += -= *= /= %= |
| 660 | /// ^= &= |= << >> >>= <<= == != |
| 661 | /// <= >= && || ++ -- , ->* -> |
| 662 | /// () [] |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 663 | OverloadedOperatorKind |
| 664 | Parser::TryParseOperatorFunctionId(SourceLocation *EndLoc) { |
Argyrios Kyrtzidis | 9057a81 | 2008-11-07 15:54:02 +0000 | [diff] [blame] | 665 | assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword"); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 666 | SourceLocation Loc; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 667 | |
| 668 | OverloadedOperatorKind Op = OO_None; |
| 669 | switch (NextToken().getKind()) { |
| 670 | case tok::kw_new: |
| 671 | ConsumeToken(); // 'operator' |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 672 | Loc = ConsumeToken(); // 'new' |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 673 | if (Tok.is(tok::l_square)) { |
| 674 | ConsumeBracket(); // '[' |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 675 | Loc = Tok.getLocation(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 676 | ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']' |
| 677 | Op = OO_Array_New; |
| 678 | } else { |
| 679 | Op = OO_New; |
| 680 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 681 | if (EndLoc) |
| 682 | *EndLoc = Loc; |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 683 | return Op; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 684 | |
| 685 | case tok::kw_delete: |
| 686 | ConsumeToken(); // 'operator' |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 687 | Loc = ConsumeToken(); // 'delete' |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 688 | if (Tok.is(tok::l_square)) { |
| 689 | ConsumeBracket(); // '[' |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 690 | Loc = Tok.getLocation(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 691 | ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']' |
| 692 | Op = OO_Array_Delete; |
| 693 | } else { |
| 694 | Op = OO_Delete; |
| 695 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 696 | if (EndLoc) |
| 697 | *EndLoc = Loc; |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 698 | return Op; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 699 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 700 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 701 | case tok::Token: Op = OO_##Name; break; |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 702 | #define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly) |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 703 | #include "clang/Basic/OperatorKinds.def" |
| 704 | |
| 705 | case tok::l_paren: |
| 706 | ConsumeToken(); // 'operator' |
| 707 | ConsumeParen(); // '(' |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 708 | Loc = Tok.getLocation(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 709 | ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); // ')' |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 710 | if (EndLoc) |
| 711 | *EndLoc = Loc; |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 712 | return OO_Call; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 713 | |
| 714 | case tok::l_square: |
| 715 | ConsumeToken(); // 'operator' |
| 716 | ConsumeBracket(); // '[' |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 717 | Loc = Tok.getLocation(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 718 | ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']' |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 719 | if (EndLoc) |
| 720 | *EndLoc = Loc; |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 721 | return OO_Subscript; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 722 | |
| 723 | default: |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 724 | return OO_None; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 727 | ConsumeToken(); // 'operator' |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 728 | Loc = ConsumeAnyToken(); // the operator itself |
| 729 | if (EndLoc) |
| 730 | *EndLoc = Loc; |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 731 | return Op; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 732 | } |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 733 | |
| 734 | /// ParseConversionFunctionId - Parse a C++ conversion-function-id, |
| 735 | /// which expresses the name of a user-defined conversion operator |
| 736 | /// (C++ [class.conv.fct]p1). Returns the type that this operator is |
| 737 | /// specifying a conversion for, or NULL if there was an error. |
| 738 | /// |
| 739 | /// conversion-function-id: [C++ 12.3.2] |
| 740 | /// operator conversion-type-id |
| 741 | /// |
| 742 | /// conversion-type-id: |
| 743 | /// type-specifier-seq conversion-declarator[opt] |
| 744 | /// |
| 745 | /// conversion-declarator: |
| 746 | /// ptr-operator conversion-declarator[opt] |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 747 | Parser::TypeTy *Parser::ParseConversionFunctionId(SourceLocation *EndLoc) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 748 | assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword"); |
| 749 | ConsumeToken(); // 'operator' |
| 750 | |
| 751 | // Parse the type-specifier-seq. |
| 752 | DeclSpec DS; |
| 753 | if (ParseCXXTypeSpecifierSeq(DS)) |
| 754 | return 0; |
| 755 | |
| 756 | // Parse the conversion-declarator, which is merely a sequence of |
| 757 | // ptr-operators. |
| 758 | Declarator D(DS, Declarator::TypeNameContext); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 759 | ParseDeclaratorInternal(D, /*DirectDeclParser=*/0); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 760 | if (EndLoc) |
| 761 | *EndLoc = D.getSourceRange().getEnd(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 762 | |
| 763 | // Finish up the type. |
| 764 | Action::TypeResult Result = Actions.ActOnTypeName(CurScope, D); |
Douglas Gregor | 5ac8aff | 2009-01-26 22:44:13 +0000 | [diff] [blame] | 765 | if (Result.isInvalid()) |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 766 | return 0; |
| 767 | else |
Douglas Gregor | 5ac8aff | 2009-01-26 22:44:13 +0000 | [diff] [blame] | 768 | return Result.get(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 769 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 770 | |
| 771 | /// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate |
| 772 | /// memory in a typesafe manner and call constructors. |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 773 | /// |
| 774 | /// This method is called to parse the new expression after the optional :: has |
| 775 | /// been already parsed. If the :: was present, "UseGlobal" is true and "Start" |
| 776 | /// is its location. Otherwise, "Start" is the location of the 'new' token. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 777 | /// |
| 778 | /// new-expression: |
| 779 | /// '::'[opt] 'new' new-placement[opt] new-type-id |
| 780 | /// new-initializer[opt] |
| 781 | /// '::'[opt] 'new' new-placement[opt] '(' type-id ')' |
| 782 | /// new-initializer[opt] |
| 783 | /// |
| 784 | /// new-placement: |
| 785 | /// '(' expression-list ')' |
| 786 | /// |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 787 | /// new-type-id: |
| 788 | /// type-specifier-seq new-declarator[opt] |
| 789 | /// |
| 790 | /// new-declarator: |
| 791 | /// ptr-operator new-declarator[opt] |
| 792 | /// direct-new-declarator |
| 793 | /// |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 794 | /// new-initializer: |
| 795 | /// '(' expression-list[opt] ')' |
| 796 | /// [C++0x] braced-init-list [TODO] |
| 797 | /// |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 798 | Parser::OwningExprResult |
| 799 | Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) { |
| 800 | assert(Tok.is(tok::kw_new) && "expected 'new' token"); |
| 801 | ConsumeToken(); // Consume 'new' |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 802 | |
| 803 | // A '(' now can be a new-placement or the '(' wrapping the type-id in the |
| 804 | // second form of new-expression. It can't be a new-type-id. |
| 805 | |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 806 | ExprVector PlacementArgs(Actions); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 807 | SourceLocation PlacementLParen, PlacementRParen; |
| 808 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 809 | bool ParenTypeId; |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 810 | DeclSpec DS; |
| 811 | Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 812 | if (Tok.is(tok::l_paren)) { |
| 813 | // If it turns out to be a placement, we change the type location. |
| 814 | PlacementLParen = ConsumeParen(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 815 | if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) { |
| 816 | SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 817 | return ExprError(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 818 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 819 | |
| 820 | PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 821 | if (PlacementRParen.isInvalid()) { |
| 822 | SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 823 | return ExprError(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 824 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 825 | |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 826 | if (PlacementArgs.empty()) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 827 | // Reset the placement locations. There was no placement. |
| 828 | PlacementLParen = PlacementRParen = SourceLocation(); |
| 829 | ParenTypeId = true; |
| 830 | } else { |
| 831 | // We still need the type. |
| 832 | if (Tok.is(tok::l_paren)) { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 833 | SourceLocation LParen = ConsumeParen(); |
| 834 | ParseSpecifierQualifierList(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 835 | DeclaratorInfo.SetSourceRange(DS.getSourceRange()); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 836 | ParseDeclarator(DeclaratorInfo); |
| 837 | MatchRHSPunctuation(tok::r_paren, LParen); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 838 | ParenTypeId = true; |
| 839 | } else { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 840 | if (ParseCXXTypeSpecifierSeq(DS)) |
| 841 | DeclaratorInfo.setInvalidType(true); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 842 | else { |
| 843 | DeclaratorInfo.SetSourceRange(DS.getSourceRange()); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 844 | ParseDeclaratorInternal(DeclaratorInfo, |
| 845 | &Parser::ParseDirectNewDeclarator); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 846 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 847 | ParenTypeId = false; |
| 848 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 849 | } |
| 850 | } else { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 851 | // A new-type-id is a simplified type-id, where essentially the |
| 852 | // direct-declarator is replaced by a direct-new-declarator. |
| 853 | if (ParseCXXTypeSpecifierSeq(DS)) |
| 854 | DeclaratorInfo.setInvalidType(true); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 855 | else { |
| 856 | DeclaratorInfo.SetSourceRange(DS.getSourceRange()); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 857 | ParseDeclaratorInternal(DeclaratorInfo, |
| 858 | &Parser::ParseDirectNewDeclarator); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 859 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 860 | ParenTypeId = false; |
| 861 | } |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 862 | if (DeclaratorInfo.getInvalidType()) { |
| 863 | SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 864 | return ExprError(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 865 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 866 | |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 867 | ExprVector ConstructorArgs(Actions); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 868 | SourceLocation ConstructorLParen, ConstructorRParen; |
| 869 | |
| 870 | if (Tok.is(tok::l_paren)) { |
| 871 | ConstructorLParen = ConsumeParen(); |
| 872 | if (Tok.isNot(tok::r_paren)) { |
| 873 | CommaLocsTy CommaLocs; |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 874 | if (ParseExpressionList(ConstructorArgs, CommaLocs)) { |
| 875 | SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 876 | return ExprError(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 877 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 878 | } |
| 879 | ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 880 | if (ConstructorRParen.isInvalid()) { |
| 881 | SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 882 | return ExprError(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 883 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 884 | } |
| 885 | |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 886 | return Owned(Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen, |
| 887 | PlacementArgs.take(), PlacementArgs.size(), |
| 888 | PlacementRParen, ParenTypeId, DeclaratorInfo, |
| 889 | ConstructorLParen, ConstructorArgs.take(), |
| 890 | ConstructorArgs.size(), ConstructorRParen)); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 891 | } |
| 892 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 893 | /// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be |
| 894 | /// passed to ParseDeclaratorInternal. |
| 895 | /// |
| 896 | /// direct-new-declarator: |
| 897 | /// '[' expression ']' |
| 898 | /// direct-new-declarator '[' constant-expression ']' |
| 899 | /// |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 900 | void Parser::ParseDirectNewDeclarator(Declarator &D) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 901 | // Parse the array dimensions. |
| 902 | bool first = true; |
| 903 | while (Tok.is(tok::l_square)) { |
| 904 | SourceLocation LLoc = ConsumeBracket(); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 905 | OwningExprResult Size(first ? ParseExpression() |
| 906 | : ParseConstantExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 907 | if (Size.isInvalid()) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 908 | // Recover |
| 909 | SkipUntil(tok::r_square); |
| 910 | return; |
| 911 | } |
| 912 | first = false; |
| 913 | |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 914 | SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 915 | D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false, |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 916 | Size.release(), LLoc), |
| 917 | RLoc); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 918 | |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 919 | if (RLoc.isInvalid()) |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 920 | return; |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | /// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id. |
| 925 | /// This ambiguity appears in the syntax of the C++ new operator. |
| 926 | /// |
| 927 | /// new-expression: |
| 928 | /// '::'[opt] 'new' new-placement[opt] '(' type-id ')' |
| 929 | /// new-initializer[opt] |
| 930 | /// |
| 931 | /// new-placement: |
| 932 | /// '(' expression-list ')' |
| 933 | /// |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 934 | bool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs, |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 935 | Declarator &D) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 936 | // The '(' was already consumed. |
| 937 | if (isTypeIdInParens()) { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 938 | ParseSpecifierQualifierList(D.getMutableDeclSpec()); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 939 | D.SetSourceRange(D.getDeclSpec().getSourceRange()); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 940 | ParseDeclarator(D); |
| 941 | return D.getInvalidType(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | // It's not a type, it has to be an expression list. |
| 945 | // Discard the comma locations - ActOnCXXNew has enough parameters. |
| 946 | CommaLocsTy CommaLocs; |
| 947 | return ParseExpressionList(PlacementArgs, CommaLocs); |
| 948 | } |
| 949 | |
| 950 | /// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used |
| 951 | /// to free memory allocated by new. |
| 952 | /// |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 953 | /// This method is called to parse the 'delete' expression after the optional |
| 954 | /// '::' has been already parsed. If the '::' was present, "UseGlobal" is true |
| 955 | /// and "Start" is its location. Otherwise, "Start" is the location of the |
| 956 | /// 'delete' token. |
| 957 | /// |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 958 | /// delete-expression: |
| 959 | /// '::'[opt] 'delete' cast-expression |
| 960 | /// '::'[opt] 'delete' '[' ']' cast-expression |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 961 | Parser::OwningExprResult |
| 962 | Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) { |
| 963 | assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword"); |
| 964 | ConsumeToken(); // Consume 'delete' |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 965 | |
| 966 | // Array delete? |
| 967 | bool ArrayDelete = false; |
| 968 | if (Tok.is(tok::l_square)) { |
| 969 | ArrayDelete = true; |
| 970 | SourceLocation LHS = ConsumeBracket(); |
| 971 | SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS); |
| 972 | if (RHS.isInvalid()) |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 973 | return ExprError(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 974 | } |
| 975 | |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 976 | OwningExprResult Operand(ParseCastExpression(false)); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 977 | if (Operand.isInvalid()) |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 978 | return move(Operand); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 979 | |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 980 | return Owned(Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, |
| 981 | Operand.release())); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 982 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 983 | |
| 984 | static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) |
| 985 | { |
| 986 | switch(kind) { |
| 987 | default: assert(false && "Not a known unary type trait."); |
| 988 | case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign; |
| 989 | case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy; |
| 990 | case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor; |
| 991 | case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign; |
| 992 | case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy; |
| 993 | case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor; |
| 994 | case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor; |
| 995 | case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor; |
| 996 | case tok::kw___is_abstract: return UTT_IsAbstract; |
| 997 | case tok::kw___is_class: return UTT_IsClass; |
| 998 | case tok::kw___is_empty: return UTT_IsEmpty; |
| 999 | case tok::kw___is_enum: return UTT_IsEnum; |
| 1000 | case tok::kw___is_pod: return UTT_IsPOD; |
| 1001 | case tok::kw___is_polymorphic: return UTT_IsPolymorphic; |
| 1002 | case tok::kw___is_union: return UTT_IsUnion; |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | /// ParseUnaryTypeTrait - Parse the built-in unary type-trait |
| 1007 | /// pseudo-functions that allow implementation of the TR1/C++0x type traits |
| 1008 | /// templates. |
| 1009 | /// |
| 1010 | /// primary-expression: |
| 1011 | /// [GNU] unary-type-trait '(' type-id ')' |
| 1012 | /// |
| 1013 | Parser::OwningExprResult Parser::ParseUnaryTypeTrait() |
| 1014 | { |
| 1015 | UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind()); |
| 1016 | SourceLocation Loc = ConsumeToken(); |
| 1017 | |
| 1018 | SourceLocation LParen = Tok.getLocation(); |
| 1019 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen)) |
| 1020 | return ExprError(); |
| 1021 | |
| 1022 | // FIXME: Error reporting absolutely sucks! If the this fails to parse a type |
| 1023 | // there will be cryptic errors about mismatched parentheses and missing |
| 1024 | // specifiers. |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 1025 | TypeResult Ty = ParseTypeName(); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1026 | |
| 1027 | SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen); |
| 1028 | |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 1029 | if (Ty.isInvalid()) |
| 1030 | return ExprError(); |
| 1031 | |
| 1032 | return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty.get(), RParen); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1033 | } |