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