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