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