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