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