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" |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 17 | #include "llvm/Support/ErrorHandling.h" |
| 18 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 19 | using namespace clang; |
| 20 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 21 | /// \brief Parse global scope or nested-name-specifier if present. |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 22 | /// |
| 23 | /// Parses a C++ global scope specifier ('::') or nested-name-specifier (which |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 24 | /// 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] | 25 | /// ::delete; it will just leave them in the token stream. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 26 | /// |
| 27 | /// '::'[opt] nested-name-specifier |
| 28 | /// '::' |
| 29 | /// |
| 30 | /// nested-name-specifier: |
| 31 | /// type-name '::' |
| 32 | /// namespace-name '::' |
| 33 | /// nested-name-specifier identifier '::' |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 34 | /// nested-name-specifier 'template'[opt] simple-template-id '::' |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 35 | /// |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 36 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 37 | /// \param SS the scope specifier that will be set to the parsed |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 38 | /// nested-name-specifier (or empty) |
| 39 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 40 | /// \param ObjectType if this nested-name-specifier is being parsed following |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 41 | /// the "." or "->" of a member access expression, this parameter provides the |
| 42 | /// type of the object whose members are being accessed. |
| 43 | /// |
| 44 | /// \param EnteringContext whether we will be entering into the context of |
| 45 | /// the nested-name-specifier after parsing it. |
| 46 | /// |
| 47 | /// \returns true if a scope specifier was parsed. |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 48 | bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS, |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 49 | Action::TypeTy *ObjectType, |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 50 | bool EnteringContext) { |
Argyrios Kyrtzidis | 4bdd91c | 2008-11-26 21:41:52 +0000 | [diff] [blame] | 51 | assert(getLang().CPlusPlus && |
Chris Lattner | 7452c6f | 2009-01-05 01:24:05 +0000 | [diff] [blame] | 52 | "Call sites of this function should be guarded by checking for C++"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 53 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 54 | if (Tok.is(tok::annot_cxxscope)) { |
Douglas Gregor | 3507369 | 2009-03-26 23:56:24 +0000 | [diff] [blame] | 55 | SS.setScopeRep(Tok.getAnnotationValue()); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 56 | SS.setRange(Tok.getAnnotationRange()); |
| 57 | ConsumeToken(); |
Argyrios Kyrtzidis | 4bdd91c | 2008-11-26 21:41:52 +0000 | [diff] [blame] | 58 | return true; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 59 | } |
Chris Lattner | e607e80 | 2009-01-04 21:14:15 +0000 | [diff] [blame] | 60 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 61 | bool HasScopeSpecifier = false; |
| 62 | |
Chris Lattner | 5b45473 | 2009-01-05 03:55:46 +0000 | [diff] [blame] | 63 | if (Tok.is(tok::coloncolon)) { |
| 64 | // ::new and ::delete aren't nested-name-specifiers. |
| 65 | tok::TokenKind NextKind = NextToken().getKind(); |
| 66 | if (NextKind == tok::kw_new || NextKind == tok::kw_delete) |
| 67 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 68 | |
Chris Lattner | 55a7cef | 2009-01-05 00:13:00 +0000 | [diff] [blame] | 69 | // '::' - Global scope qualifier. |
Chris Lattner | 357089d | 2009-01-05 02:07:19 +0000 | [diff] [blame] | 70 | SourceLocation CCLoc = ConsumeToken(); |
Chris Lattner | 357089d | 2009-01-05 02:07:19 +0000 | [diff] [blame] | 71 | SS.setBeginLoc(CCLoc); |
Douglas Gregor | 3507369 | 2009-03-26 23:56:24 +0000 | [diff] [blame] | 72 | SS.setScopeRep(Actions.ActOnCXXGlobalScopeSpecifier(CurScope, CCLoc)); |
Chris Lattner | 357089d | 2009-01-05 02:07:19 +0000 | [diff] [blame] | 73 | SS.setEndLoc(CCLoc); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 74 | HasScopeSpecifier = true; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 77 | while (true) { |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 78 | if (HasScopeSpecifier) { |
| 79 | // C++ [basic.lookup.classref]p5: |
| 80 | // If the qualified-id has the form |
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 | // ::class-name-or-namespace-name::... |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 83 | // |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 84 | // the class-name-or-namespace-name is looked up in global scope as a |
| 85 | // class-name or namespace-name. |
| 86 | // |
| 87 | // To implement this, we clear out the object type as soon as we've |
| 88 | // seen a leading '::' or part of a nested-name-specifier. |
| 89 | ObjectType = 0; |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 90 | |
| 91 | if (Tok.is(tok::code_completion)) { |
| 92 | // Code completion for a nested-name-specifier, where the code |
| 93 | // code completion token follows the '::'. |
| 94 | Actions.CodeCompleteQualifiedId(CurScope, SS, EnteringContext); |
| 95 | ConsumeToken(); |
| 96 | } |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 97 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 98 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 99 | // nested-name-specifier: |
Chris Lattner | 77cf72a | 2009-06-26 03:47:46 +0000 | [diff] [blame] | 100 | // nested-name-specifier 'template'[opt] simple-template-id '::' |
| 101 | |
| 102 | // Parse the optional 'template' keyword, then make sure we have |
| 103 | // 'identifier <' after it. |
| 104 | if (Tok.is(tok::kw_template)) { |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 105 | // 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] | 106 | // nested-name-specifier, since they aren't allowed to start with |
| 107 | // 'template'. |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 108 | if (!HasScopeSpecifier && !ObjectType) |
Eli Friedman | eab975d | 2009-08-29 04:08:08 +0000 | [diff] [blame] | 109 | break; |
| 110 | |
Chris Lattner | 77cf72a | 2009-06-26 03:47:46 +0000 | [diff] [blame] | 111 | SourceLocation TemplateKWLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 112 | |
Chris Lattner | 77cf72a | 2009-06-26 03:47:46 +0000 | [diff] [blame] | 113 | if (Tok.isNot(tok::identifier)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 114 | Diag(Tok.getLocation(), |
Chris Lattner | 77cf72a | 2009-06-26 03:47:46 +0000 | [diff] [blame] | 115 | diag::err_id_after_template_in_nested_name_spec) |
| 116 | << SourceRange(TemplateKWLoc); |
| 117 | break; |
| 118 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 119 | |
Chris Lattner | 77cf72a | 2009-06-26 03:47:46 +0000 | [diff] [blame] | 120 | if (NextToken().isNot(tok::less)) { |
| 121 | Diag(NextToken().getLocation(), |
| 122 | diag::err_less_after_template_name_in_nested_name_spec) |
| 123 | << Tok.getIdentifierInfo()->getName() |
| 124 | << SourceRange(TemplateKWLoc, Tok.getLocation()); |
| 125 | break; |
| 126 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 127 | |
| 128 | TemplateTy Template |
Chris Lattner | 77cf72a | 2009-06-26 03:47:46 +0000 | [diff] [blame] | 129 | = Actions.ActOnDependentTemplateName(TemplateKWLoc, |
| 130 | *Tok.getIdentifierInfo(), |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 131 | Tok.getLocation(), SS, |
| 132 | ObjectType); |
Eli Friedman | eab975d | 2009-08-29 04:08:08 +0000 | [diff] [blame] | 133 | if (!Template) |
| 134 | break; |
Chris Lattner | c8e27cc | 2009-06-26 04:27:47 +0000 | [diff] [blame] | 135 | if (AnnotateTemplateIdToken(Template, TNK_Dependent_template_name, |
| 136 | &SS, TemplateKWLoc, false)) |
| 137 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 138 | |
Chris Lattner | 77cf72a | 2009-06-26 03:47:46 +0000 | [diff] [blame] | 139 | continue; |
| 140 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 141 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 142 | if (Tok.is(tok::annot_template_id) && NextToken().is(tok::coloncolon)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 143 | // We have |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 144 | // |
| 145 | // simple-template-id '::' |
| 146 | // |
| 147 | // 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] | 148 | // right kind (it should name a type or be dependent), and then |
| 149 | // convert it into a type within the nested-name-specifier. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 150 | TemplateIdAnnotation *TemplateId |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 151 | = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); |
| 152 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 153 | if (TemplateId->Kind == TNK_Type_template || |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 154 | TemplateId->Kind == TNK_Dependent_template_name) { |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 155 | AnnotateTemplateIdTokenAsType(&SS); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 156 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 157 | assert(Tok.is(tok::annot_typename) && |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 158 | "AnnotateTemplateIdTokenAsType isn't working"); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 159 | Token TypeToken = Tok; |
| 160 | ConsumeToken(); |
| 161 | assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!"); |
| 162 | SourceLocation CCLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 163 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 164 | if (!HasScopeSpecifier) { |
| 165 | SS.setBeginLoc(TypeToken.getLocation()); |
| 166 | HasScopeSpecifier = true; |
| 167 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 168 | |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 169 | if (TypeToken.getAnnotationValue()) |
| 170 | SS.setScopeRep( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 171 | Actions.ActOnCXXNestedNameSpecifier(CurScope, SS, |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 172 | TypeToken.getAnnotationValue(), |
| 173 | TypeToken.getAnnotationRange(), |
| 174 | CCLoc)); |
| 175 | else |
| 176 | SS.setScopeRep(0); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 177 | SS.setEndLoc(CCLoc); |
| 178 | continue; |
Chris Lattner | 67b9e83 | 2009-06-26 03:45:46 +0000 | [diff] [blame] | 179 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 180 | |
Chris Lattner | 67b9e83 | 2009-06-26 03:45:46 +0000 | [diff] [blame] | 181 | assert(false && "FIXME: Only type template names supported here"); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Chris Lattner | 5c7f786 | 2009-06-26 03:52:38 +0000 | [diff] [blame] | 184 | |
| 185 | // The rest of the nested-name-specifier possibilities start with |
| 186 | // tok::identifier. |
| 187 | if (Tok.isNot(tok::identifier)) |
| 188 | break; |
| 189 | |
| 190 | IdentifierInfo &II = *Tok.getIdentifierInfo(); |
| 191 | |
| 192 | // nested-name-specifier: |
| 193 | // type-name '::' |
| 194 | // namespace-name '::' |
| 195 | // nested-name-specifier identifier '::' |
| 196 | Token Next = NextToken(); |
| 197 | if (Next.is(tok::coloncolon)) { |
| 198 | // We have an identifier followed by a '::'. Lookup this name |
| 199 | // as the name in a nested-name-specifier. |
| 200 | SourceLocation IdLoc = ConsumeToken(); |
| 201 | assert(Tok.is(tok::coloncolon) && "NextToken() not working properly!"); |
| 202 | SourceLocation CCLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 203 | |
Chris Lattner | 5c7f786 | 2009-06-26 03:52:38 +0000 | [diff] [blame] | 204 | if (!HasScopeSpecifier) { |
| 205 | SS.setBeginLoc(IdLoc); |
| 206 | HasScopeSpecifier = true; |
| 207 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 208 | |
Chris Lattner | 5c7f786 | 2009-06-26 03:52:38 +0000 | [diff] [blame] | 209 | if (SS.isInvalid()) |
| 210 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 211 | |
Chris Lattner | 5c7f786 | 2009-06-26 03:52:38 +0000 | [diff] [blame] | 212 | SS.setScopeRep( |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 213 | Actions.ActOnCXXNestedNameSpecifier(CurScope, SS, IdLoc, CCLoc, II, |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 214 | ObjectType, EnteringContext)); |
Chris Lattner | 5c7f786 | 2009-06-26 03:52:38 +0000 | [diff] [blame] | 215 | SS.setEndLoc(CCLoc); |
| 216 | continue; |
| 217 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 218 | |
Chris Lattner | 5c7f786 | 2009-06-26 03:52:38 +0000 | [diff] [blame] | 219 | // nested-name-specifier: |
| 220 | // type-name '<' |
| 221 | if (Next.is(tok::less)) { |
| 222 | TemplateTy Template; |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 223 | if (TemplateNameKind TNK = Actions.isTemplateName(CurScope, II, |
| 224 | Tok.getLocation(), |
| 225 | &SS, |
| 226 | ObjectType, |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 227 | EnteringContext, |
| 228 | Template)) { |
Chris Lattner | 5c7f786 | 2009-06-26 03:52:38 +0000 | [diff] [blame] | 229 | // We have found a template name, so annotate this this token |
| 230 | // with a template-id annotation. We do not permit the |
| 231 | // template-id to be translated into a type annotation, |
| 232 | // because some clients (e.g., the parsing of class template |
| 233 | // specializations) still want to see the original template-id |
| 234 | // token. |
Chris Lattner | c8e27cc | 2009-06-26 04:27:47 +0000 | [diff] [blame] | 235 | if (AnnotateTemplateIdToken(Template, TNK, &SS, SourceLocation(), |
| 236 | false)) |
| 237 | break; |
Chris Lattner | 5c7f786 | 2009-06-26 03:52:38 +0000 | [diff] [blame] | 238 | continue; |
| 239 | } |
| 240 | } |
| 241 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 242 | // We don't have any tokens that form the beginning of a |
| 243 | // nested-name-specifier, so we're done. |
| 244 | break; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 245 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 246 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 247 | return HasScopeSpecifier; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | /// ParseCXXIdExpression - Handle id-expression. |
| 251 | /// |
| 252 | /// id-expression: |
| 253 | /// unqualified-id |
| 254 | /// qualified-id |
| 255 | /// |
| 256 | /// unqualified-id: |
| 257 | /// identifier |
| 258 | /// operator-function-id |
| 259 | /// conversion-function-id [TODO] |
| 260 | /// '~' class-name [TODO] |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 261 | /// template-id |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 262 | /// |
| 263 | /// qualified-id: |
| 264 | /// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id |
| 265 | /// '::' identifier |
| 266 | /// '::' operator-function-id |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 267 | /// '::' template-id |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 268 | /// |
| 269 | /// nested-name-specifier: |
| 270 | /// type-name '::' |
| 271 | /// namespace-name '::' |
| 272 | /// nested-name-specifier identifier '::' |
| 273 | /// nested-name-specifier 'template'[opt] simple-template-id '::' [TODO] |
| 274 | /// |
| 275 | /// NOTE: The standard specifies that, for qualified-id, the parser does not |
| 276 | /// expect: |
| 277 | /// |
| 278 | /// '::' conversion-function-id |
| 279 | /// '::' '~' class-name |
| 280 | /// |
| 281 | /// This may cause a slight inconsistency on diagnostics: |
| 282 | /// |
| 283 | /// class C {}; |
| 284 | /// namespace A {} |
| 285 | /// void f() { |
| 286 | /// :: A :: ~ C(); // Some Sema error about using destructor with a |
| 287 | /// // namespace. |
| 288 | /// :: ~ C(); // Some Parser error like 'unexpected ~'. |
| 289 | /// } |
| 290 | /// |
| 291 | /// We simplify the parser a bit and make it work like: |
| 292 | /// |
| 293 | /// qualified-id: |
| 294 | /// '::'[opt] nested-name-specifier 'template'[opt] unqualified-id |
| 295 | /// '::' unqualified-id |
| 296 | /// |
| 297 | /// That way Sema can handle and report similar errors for namespaces and the |
| 298 | /// global scope. |
| 299 | /// |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 300 | /// The isAddressOfOperand parameter indicates that this id-expression is a |
| 301 | /// direct operand of the address-of operator. This is, besides member contexts, |
| 302 | /// the only place where a qualified-id naming a non-static class member may |
| 303 | /// appear. |
| 304 | /// |
| 305 | Parser::OwningExprResult Parser::ParseCXXIdExpression(bool isAddressOfOperand) { |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 306 | // qualified-id: |
| 307 | // '::'[opt] nested-name-specifier 'template'[opt] unqualified-id |
| 308 | // '::' unqualified-id |
| 309 | // |
| 310 | CXXScopeSpec SS; |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 311 | ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, false); |
Douglas Gregor | 02a24ee | 2009-11-03 16:56:39 +0000 | [diff] [blame^] | 312 | |
| 313 | UnqualifiedId Name; |
| 314 | if (ParseUnqualifiedId(SS, |
| 315 | /*EnteringContext=*/false, |
| 316 | /*AllowDestructorName=*/false, |
| 317 | /*AllowConstructorName=*/false, |
| 318 | Name)) |
| 319 | return ExprError(); |
| 320 | |
| 321 | return Actions.ActOnIdExpression(CurScope, SS, Name, Tok.is(tok::l_paren), |
| 322 | isAddressOfOperand); |
| 323 | |
| 324 | #if 0 |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 325 | // unqualified-id: |
| 326 | // identifier |
| 327 | // operator-function-id |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 328 | // conversion-function-id |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 329 | // '~' class-name [TODO] |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 330 | // template-id |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 331 | // |
| 332 | switch (Tok.getKind()) { |
| 333 | default: |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 334 | return ExprError(Diag(Tok, diag::err_expected_unqualified_id)); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 335 | |
| 336 | case tok::identifier: { |
| 337 | // Consume the identifier so that we can see if it is followed by a '('. |
| 338 | IdentifierInfo &II = *Tok.getIdentifierInfo(); |
| 339 | SourceLocation L = ConsumeToken(); |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 340 | return Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren), |
| 341 | &SS, isAddressOfOperand); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | case tok::kw_operator: { |
| 345 | SourceLocation OperatorLoc = Tok.getLocation(); |
Chris Lattner | 7452c6f | 2009-01-05 01:24:05 +0000 | [diff] [blame] | 346 | if (OverloadedOperatorKind Op = TryParseOperatorFunctionId()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 347 | return Actions.ActOnCXXOperatorFunctionIdExpr( |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 348 | CurScope, OperatorLoc, Op, Tok.is(tok::l_paren), SS, |
| 349 | isAddressOfOperand); |
Chris Lattner | 7452c6f | 2009-01-05 01:24:05 +0000 | [diff] [blame] | 350 | if (TypeTy *Type = ParseConversionFunctionId()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 351 | return Actions.ActOnCXXConversionFunctionExpr(CurScope, OperatorLoc, Type, |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 352 | Tok.is(tok::l_paren), SS, |
| 353 | isAddressOfOperand); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 354 | |
Douglas Gregor | 2def483 | 2008-11-17 20:34:05 +0000 | [diff] [blame] | 355 | // We already complained about a bad conversion-function-id, |
| 356 | // above. |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 357 | return ExprError(); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 360 | case tok::annot_template_id: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 361 | TemplateIdAnnotation *TemplateId |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 362 | = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); |
| 363 | assert((TemplateId->Kind == TNK_Function_template || |
| 364 | TemplateId->Kind == TNK_Dependent_template_name) && |
| 365 | "A template type name is not an ID expression"); |
| 366 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 367 | ASTTemplateArgsPtr TemplateArgsPtr(Actions, |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 368 | TemplateId->getTemplateArgs(), |
| 369 | TemplateId->getTemplateArgIsType(), |
| 370 | TemplateId->NumArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 371 | |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 372 | OwningExprResult Result |
Douglas Gregor | f17bb74 | 2009-10-22 17:20:55 +0000 | [diff] [blame] | 373 | = Actions.ActOnTemplateIdExpr(SS, |
| 374 | TemplateTy::make(TemplateId->Template), |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 375 | TemplateId->TemplateNameLoc, |
| 376 | TemplateId->LAngleLoc, |
| 377 | TemplateArgsPtr, |
| 378 | TemplateId->getTemplateArgLocations(), |
| 379 | TemplateId->RAngleLoc); |
| 380 | ConsumeToken(); // Consume the template-id token |
| 381 | return move(Result); |
| 382 | } |
| 383 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 384 | } // switch. |
| 385 | |
| 386 | assert(0 && "The switch was supposed to take care everything."); |
Douglas Gregor | 02a24ee | 2009-11-03 16:56:39 +0000 | [diff] [blame^] | 387 | #endif |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 390 | /// ParseCXXCasts - This handles the various ways to cast expressions to another |
| 391 | /// type. |
| 392 | /// |
| 393 | /// postfix-expression: [C++ 5.2p1] |
| 394 | /// 'dynamic_cast' '<' type-name '>' '(' expression ')' |
| 395 | /// 'static_cast' '<' type-name '>' '(' expression ')' |
| 396 | /// 'reinterpret_cast' '<' type-name '>' '(' expression ')' |
| 397 | /// 'const_cast' '<' type-name '>' '(' expression ')' |
| 398 | /// |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 399 | Parser::OwningExprResult Parser::ParseCXXCasts() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 400 | tok::TokenKind Kind = Tok.getKind(); |
| 401 | const char *CastName = 0; // For error messages |
| 402 | |
| 403 | switch (Kind) { |
| 404 | default: assert(0 && "Unknown C++ cast!"); abort(); |
| 405 | case tok::kw_const_cast: CastName = "const_cast"; break; |
| 406 | case tok::kw_dynamic_cast: CastName = "dynamic_cast"; break; |
| 407 | case tok::kw_reinterpret_cast: CastName = "reinterpret_cast"; break; |
| 408 | case tok::kw_static_cast: CastName = "static_cast"; break; |
| 409 | } |
| 410 | |
| 411 | SourceLocation OpLoc = ConsumeToken(); |
| 412 | SourceLocation LAngleBracketLoc = Tok.getLocation(); |
| 413 | |
| 414 | if (ExpectAndConsume(tok::less, diag::err_expected_less_after, CastName)) |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 415 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 416 | |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 417 | TypeResult CastTy = ParseTypeName(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 418 | SourceLocation RAngleBracketLoc = Tok.getLocation(); |
| 419 | |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 420 | if (ExpectAndConsume(tok::greater, diag::err_expected_greater)) |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 421 | return ExprError(Diag(LAngleBracketLoc, diag::note_matching) << "<"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 422 | |
| 423 | SourceLocation LParenLoc = Tok.getLocation(), RParenLoc; |
| 424 | |
Argyrios Kyrtzidis | 21e7ad2 | 2009-05-22 10:23:16 +0000 | [diff] [blame] | 425 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, CastName)) |
| 426 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 427 | |
Argyrios Kyrtzidis | 21e7ad2 | 2009-05-22 10:23:16 +0000 | [diff] [blame] | 428 | OwningExprResult Result = ParseExpression(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 429 | |
Argyrios Kyrtzidis | 21e7ad2 | 2009-05-22 10:23:16 +0000 | [diff] [blame] | 430 | // Match the ')'. |
| 431 | if (Result.isInvalid()) |
| 432 | SkipUntil(tok::r_paren); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 433 | |
Argyrios Kyrtzidis | 21e7ad2 | 2009-05-22 10:23:16 +0000 | [diff] [blame] | 434 | if (Tok.is(tok::r_paren)) |
| 435 | RParenLoc = ConsumeParen(); |
| 436 | else |
| 437 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 438 | |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 439 | if (!Result.isInvalid() && !CastTy.isInvalid()) |
Douglas Gregor | 49badde | 2008-10-27 19:41:14 +0000 | [diff] [blame] | 440 | Result = Actions.ActOnCXXNamedCast(OpLoc, Kind, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 441 | LAngleBracketLoc, CastTy.get(), |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 442 | RAngleBracketLoc, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 443 | LParenLoc, move(Result), RParenLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 444 | |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 445 | return move(Result); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 448 | /// ParseCXXTypeid - This handles the C++ typeid expression. |
| 449 | /// |
| 450 | /// postfix-expression: [C++ 5.2p1] |
| 451 | /// 'typeid' '(' expression ')' |
| 452 | /// 'typeid' '(' type-id ')' |
| 453 | /// |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 454 | Parser::OwningExprResult Parser::ParseCXXTypeid() { |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 455 | assert(Tok.is(tok::kw_typeid) && "Not 'typeid'!"); |
| 456 | |
| 457 | SourceLocation OpLoc = ConsumeToken(); |
| 458 | SourceLocation LParenLoc = Tok.getLocation(); |
| 459 | SourceLocation RParenLoc; |
| 460 | |
| 461 | // typeid expressions are always parenthesized. |
| 462 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 463 | "typeid")) |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 464 | return ExprError(); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 465 | |
Sebastian Redl | 15faa7f | 2008-12-09 20:22:58 +0000 | [diff] [blame] | 466 | OwningExprResult Result(Actions); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 467 | |
| 468 | if (isTypeIdInParens()) { |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 469 | TypeResult Ty = ParseTypeName(); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 470 | |
| 471 | // Match the ')'. |
| 472 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 473 | |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 474 | if (Ty.isInvalid()) |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 475 | return ExprError(); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 476 | |
| 477 | Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/true, |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 478 | Ty.get(), RParenLoc); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 479 | } else { |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 480 | // C++0x [expr.typeid]p3: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 481 | // When typeid is applied to an expression other than an lvalue of a |
| 482 | // polymorphic class type [...] The expression is an unevaluated |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 483 | // operand (Clause 5). |
| 484 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 485 | // 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] | 486 | // polymorphic class type until after we've parsed the expression, so |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 487 | // we the expression is potentially potentially evaluated. |
| 488 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 489 | Action::PotentiallyPotentiallyEvaluated); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 490 | Result = ParseExpression(); |
| 491 | |
| 492 | // Match the ')'. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 493 | if (Result.isInvalid()) |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 494 | SkipUntil(tok::r_paren); |
| 495 | else { |
| 496 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 497 | |
| 498 | Result = Actions.ActOnCXXTypeid(OpLoc, LParenLoc, /*isType=*/false, |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 499 | Result.release(), RParenLoc); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 503 | return move(Result); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 506 | /// ParseCXXBoolLiteral - This handles the C++ Boolean literals. |
| 507 | /// |
| 508 | /// boolean-literal: [C++ 2.13.5] |
| 509 | /// 'true' |
| 510 | /// 'false' |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 511 | Parser::OwningExprResult Parser::ParseCXXBoolLiteral() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 512 | tok::TokenKind Kind = Tok.getKind(); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 513 | return Actions.ActOnCXXBoolLiteral(ConsumeToken(), Kind); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 514 | } |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 515 | |
| 516 | /// ParseThrowExpression - This handles the C++ throw expression. |
| 517 | /// |
| 518 | /// throw-expression: [C++ 15] |
| 519 | /// 'throw' assignment-expression[opt] |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 520 | Parser::OwningExprResult Parser::ParseThrowExpression() { |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 521 | assert(Tok.is(tok::kw_throw) && "Not throw!"); |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 522 | SourceLocation ThrowLoc = ConsumeToken(); // Eat the throw token. |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 523 | |
Chris Lattner | 2a2819a | 2008-04-06 06:02:23 +0000 | [diff] [blame] | 524 | // If the current token isn't the start of an assignment-expression, |
| 525 | // then the expression is not present. This handles things like: |
| 526 | // "C ? throw : (void)42", which is crazy but legal. |
| 527 | switch (Tok.getKind()) { // FIXME: move this predicate somewhere common. |
| 528 | case tok::semi: |
| 529 | case tok::r_paren: |
| 530 | case tok::r_square: |
| 531 | case tok::r_brace: |
| 532 | case tok::colon: |
| 533 | case tok::comma: |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 534 | return Actions.ActOnCXXThrow(ThrowLoc, ExprArg(Actions)); |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 535 | |
Chris Lattner | 2a2819a | 2008-04-06 06:02:23 +0000 | [diff] [blame] | 536 | default: |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 537 | OwningExprResult Expr(ParseAssignmentExpression()); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 538 | if (Expr.isInvalid()) return move(Expr); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 539 | return Actions.ActOnCXXThrow(ThrowLoc, move(Expr)); |
Chris Lattner | 2a2819a | 2008-04-06 06:02:23 +0000 | [diff] [blame] | 540 | } |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 541 | } |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 542 | |
| 543 | /// ParseCXXThis - This handles the C++ 'this' pointer. |
| 544 | /// |
| 545 | /// C++ 9.3.2: In the body of a non-static member function, the keyword this is |
| 546 | /// a non-lvalue expression whose value is the address of the object for which |
| 547 | /// the function is called. |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 548 | Parser::OwningExprResult Parser::ParseCXXThis() { |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 549 | assert(Tok.is(tok::kw_this) && "Not 'this'!"); |
| 550 | SourceLocation ThisLoc = ConsumeToken(); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 551 | return Actions.ActOnCXXThis(ThisLoc); |
Argyrios Kyrtzidis | 4cc18a4 | 2008-06-24 22:12:16 +0000 | [diff] [blame] | 552 | } |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 553 | |
| 554 | /// ParseCXXTypeConstructExpression - Parse construction of a specified type. |
| 555 | /// Can be interpreted either as function-style casting ("int(x)") |
| 556 | /// or class type construction ("ClassType(x,y,z)") |
| 557 | /// or creation of a value-initialized type ("int()"). |
| 558 | /// |
| 559 | /// postfix-expression: [C++ 5.2p1] |
| 560 | /// simple-type-specifier '(' expression-list[opt] ')' [C++ 5.2.3] |
| 561 | /// typename-specifier '(' expression-list[opt] ')' [TODO] |
| 562 | /// |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 563 | Parser::OwningExprResult |
| 564 | Parser::ParseCXXTypeConstructExpression(const DeclSpec &DS) { |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 565 | Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); |
Douglas Gregor | 5ac8aff | 2009-01-26 22:44:13 +0000 | [diff] [blame] | 566 | TypeTy *TypeRep = Actions.ActOnTypeName(CurScope, DeclaratorInfo).get(); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 567 | |
| 568 | assert(Tok.is(tok::l_paren) && "Expected '('!"); |
| 569 | SourceLocation LParenLoc = ConsumeParen(); |
| 570 | |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 571 | ExprVector Exprs(Actions); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 572 | CommaLocsTy CommaLocs; |
| 573 | |
| 574 | if (Tok.isNot(tok::r_paren)) { |
| 575 | if (ParseExpressionList(Exprs, CommaLocs)) { |
| 576 | SkipUntil(tok::r_paren); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 577 | return ExprError(); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 578 | } |
| 579 | } |
| 580 | |
| 581 | // Match the ')'. |
| 582 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 583 | |
Sebastian Redl | ef0cb8e | 2009-07-29 13:50:23 +0000 | [diff] [blame] | 584 | // TypeRep could be null, if it references an invalid typedef. |
| 585 | if (!TypeRep) |
| 586 | return ExprError(); |
| 587 | |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 588 | assert((Exprs.size() == 0 || Exprs.size()-1 == CommaLocs.size())&& |
| 589 | "Unexpected number of commas!"); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 590 | return Actions.ActOnCXXTypeConstructExpr(DS.getSourceRange(), TypeRep, |
| 591 | LParenLoc, move_arg(Exprs), |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 592 | CommaLocs.data(), RParenLoc); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 595 | /// ParseCXXCondition - if/switch/while/for condition expression. |
| 596 | /// |
| 597 | /// condition: |
| 598 | /// expression |
| 599 | /// type-specifier-seq declarator '=' assignment-expression |
| 600 | /// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt] |
| 601 | /// '=' assignment-expression |
| 602 | /// |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 603 | Parser::OwningExprResult Parser::ParseCXXCondition() { |
Argyrios Kyrtzidis | a8a4598 | 2008-10-05 15:03:47 +0000 | [diff] [blame] | 604 | if (!isCXXConditionDeclaration()) |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 605 | return ParseExpression(); // expression |
| 606 | |
| 607 | SourceLocation StartLoc = Tok.getLocation(); |
| 608 | |
| 609 | // type-specifier-seq |
| 610 | DeclSpec DS; |
| 611 | ParseSpecifierQualifierList(DS); |
| 612 | |
| 613 | // declarator |
| 614 | Declarator DeclaratorInfo(DS, Declarator::ConditionContext); |
| 615 | ParseDeclarator(DeclaratorInfo); |
| 616 | |
| 617 | // simple-asm-expr[opt] |
| 618 | if (Tok.is(tok::kw_asm)) { |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 619 | SourceLocation Loc; |
| 620 | OwningExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 621 | if (AsmLabel.isInvalid()) { |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 622 | SkipUntil(tok::semi); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 623 | return ExprError(); |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 624 | } |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 625 | DeclaratorInfo.setAsmLabel(AsmLabel.release()); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 626 | DeclaratorInfo.SetRangeEnd(Loc); |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | // If attributes are present, parse them. |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 630 | if (Tok.is(tok::kw___attribute)) { |
| 631 | SourceLocation Loc; |
| 632 | AttributeList *AttrList = ParseAttributes(&Loc); |
| 633 | DeclaratorInfo.AddAttributes(AttrList, Loc); |
| 634 | } |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 635 | |
| 636 | // '=' assignment-expression |
| 637 | if (Tok.isNot(tok::equal)) |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 638 | return ExprError(Diag(Tok, diag::err_expected_equal_after_declarator)); |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 639 | SourceLocation EqualLoc = ConsumeToken(); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 640 | OwningExprResult AssignExpr(ParseAssignmentExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 641 | if (AssignExpr.isInvalid()) |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 642 | return ExprError(); |
| 643 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 644 | return Actions.ActOnCXXConditionDeclarationExpr(CurScope, StartLoc, |
| 645 | DeclaratorInfo,EqualLoc, |
| 646 | move(AssignExpr)); |
Argyrios Kyrtzidis | 71b914b | 2008-09-09 20:38:47 +0000 | [diff] [blame] | 647 | } |
| 648 | |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 649 | /// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers. |
| 650 | /// This should only be called when the current token is known to be part of |
| 651 | /// simple-type-specifier. |
| 652 | /// |
| 653 | /// simple-type-specifier: |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 654 | /// '::'[opt] nested-name-specifier[opt] type-name |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 655 | /// '::'[opt] nested-name-specifier 'template' simple-template-id [TODO] |
| 656 | /// char |
| 657 | /// wchar_t |
| 658 | /// bool |
| 659 | /// short |
| 660 | /// int |
| 661 | /// long |
| 662 | /// signed |
| 663 | /// unsigned |
| 664 | /// float |
| 665 | /// double |
| 666 | /// void |
| 667 | /// [GNU] typeof-specifier |
| 668 | /// [C++0x] auto [TODO] |
| 669 | /// |
| 670 | /// type-name: |
| 671 | /// class-name |
| 672 | /// enum-name |
| 673 | /// typedef-name |
| 674 | /// |
| 675 | void Parser::ParseCXXSimpleTypeSpecifier(DeclSpec &DS) { |
| 676 | DS.SetRangeStart(Tok.getLocation()); |
| 677 | const char *PrevSpec; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 678 | unsigned DiagID; |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 679 | SourceLocation Loc = Tok.getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 680 | |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 681 | switch (Tok.getKind()) { |
Chris Lattner | 55a7cef | 2009-01-05 00:13:00 +0000 | [diff] [blame] | 682 | case tok::identifier: // foo::bar |
| 683 | case tok::coloncolon: // ::foo::bar |
| 684 | assert(0 && "Annotation token should already be formed!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 685 | default: |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 686 | assert(0 && "Not a simple-type-specifier token!"); |
| 687 | abort(); |
Chris Lattner | 55a7cef | 2009-01-05 00:13:00 +0000 | [diff] [blame] | 688 | |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 689 | // type-name |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 690 | case tok::annot_typename: { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 691 | DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 692 | Tok.getAnnotationValue()); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 693 | break; |
| 694 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 695 | |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 696 | // builtin types |
| 697 | case tok::kw_short: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 698 | DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 699 | break; |
| 700 | case tok::kw_long: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 701 | DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, DiagID); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 702 | break; |
| 703 | case tok::kw_signed: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 704 | DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 705 | break; |
| 706 | case tok::kw_unsigned: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 707 | DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, DiagID); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 708 | break; |
| 709 | case tok::kw_void: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 710 | DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 711 | break; |
| 712 | case tok::kw_char: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 713 | DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 714 | break; |
| 715 | case tok::kw_int: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 716 | DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 717 | break; |
| 718 | case tok::kw_float: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 719 | DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 720 | break; |
| 721 | case tok::kw_double: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 722 | DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 723 | break; |
| 724 | case tok::kw_wchar_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 725 | DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 726 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 727 | case tok::kw_char16_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 728 | DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 729 | break; |
| 730 | case tok::kw_char32_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 731 | DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 732 | break; |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 733 | case tok::kw_bool: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 734 | DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 735 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 736 | |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 737 | // GNU typeof support. |
| 738 | case tok::kw_typeof: |
| 739 | ParseTypeofSpecifier(DS); |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 740 | DS.Finish(Diags, PP); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 741 | return; |
| 742 | } |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 743 | if (Tok.is(tok::annot_typename)) |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 744 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 745 | else |
| 746 | DS.SetRangeEnd(Tok.getLocation()); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 747 | ConsumeToken(); |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 748 | DS.Finish(Diags, PP); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 749 | } |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 750 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 751 | /// ParseCXXTypeSpecifierSeq - Parse a C++ type-specifier-seq (C++ |
| 752 | /// [dcl.name]), which is a non-empty sequence of type-specifiers, |
| 753 | /// e.g., "const short int". Note that the DeclSpec is *not* finished |
| 754 | /// by parsing the type-specifier-seq, because these sequences are |
| 755 | /// typically followed by some form of declarator. Returns true and |
| 756 | /// emits diagnostics if this is not a type-specifier-seq, false |
| 757 | /// otherwise. |
| 758 | /// |
| 759 | /// type-specifier-seq: [C++ 8.1] |
| 760 | /// type-specifier type-specifier-seq[opt] |
| 761 | /// |
| 762 | bool Parser::ParseCXXTypeSpecifierSeq(DeclSpec &DS) { |
| 763 | DS.SetRangeStart(Tok.getLocation()); |
| 764 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 765 | unsigned DiagID; |
| 766 | bool isInvalid = 0; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 767 | |
| 768 | // Parse one or more of the type specifiers. |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 769 | if (!ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 770 | Diag(Tok, diag::err_operator_missing_type_specifier); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 771 | return true; |
| 772 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 773 | |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 774 | while (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID)) ; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 775 | |
| 776 | return false; |
| 777 | } |
| 778 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 779 | /// \brief Finish parsing a C++ unqualified-id that is a template-id of |
| 780 | /// some form. |
| 781 | /// |
| 782 | /// This routine is invoked when a '<' is encountered after an identifier or |
| 783 | /// operator-function-id is parsed by \c ParseUnqualifiedId() to determine |
| 784 | /// whether the unqualified-id is actually a template-id. This routine will |
| 785 | /// then parse the template arguments and form the appropriate template-id to |
| 786 | /// return to the caller. |
| 787 | /// |
| 788 | /// \param SS the nested-name-specifier that precedes this template-id, if |
| 789 | /// we're actually parsing a qualified-id. |
| 790 | /// |
| 791 | /// \param Name for constructor and destructor names, this is the actual |
| 792 | /// identifier that may be a template-name. |
| 793 | /// |
| 794 | /// \param NameLoc the location of the class-name in a constructor or |
| 795 | /// destructor. |
| 796 | /// |
| 797 | /// \param EnteringContext whether we're entering the scope of the |
| 798 | /// nested-name-specifier. |
| 799 | /// |
| 800 | /// \param Id as input, describes the template-name or operator-function-id |
| 801 | /// that precedes the '<'. If template arguments were parsed successfully, |
| 802 | /// will be updated with the template-id. |
| 803 | /// |
| 804 | /// \returns true if a parse error occurred, false otherwise. |
| 805 | bool Parser::ParseUnqualifiedIdTemplateId(CXXScopeSpec &SS, |
| 806 | IdentifierInfo *Name, |
| 807 | SourceLocation NameLoc, |
| 808 | bool EnteringContext, |
| 809 | UnqualifiedId &Id) { |
| 810 | assert(Tok.is(tok::less) && "Expected '<' to finish parsing a template-id"); |
| 811 | |
| 812 | TemplateTy Template; |
| 813 | TemplateNameKind TNK = TNK_Non_template; |
| 814 | switch (Id.getKind()) { |
| 815 | case UnqualifiedId::IK_Identifier: |
| 816 | TNK = Actions.isTemplateName(CurScope, *Id.Identifier, Id.StartLocation, |
| 817 | &SS, /*ObjectType=*/0, EnteringContext, |
| 818 | Template); |
| 819 | break; |
| 820 | |
| 821 | case UnqualifiedId::IK_OperatorFunctionId: { |
| 822 | // FIXME: Temporary hack: warn that we are completely ignoring the |
| 823 | // template arguments for now. |
| 824 | // Parse the enclosed template argument list. |
| 825 | SourceLocation LAngleLoc, RAngleLoc; |
| 826 | TemplateArgList TemplateArgs; |
| 827 | TemplateArgIsTypeList TemplateArgIsType; |
| 828 | TemplateArgLocationList TemplateArgLocations; |
| 829 | if (ParseTemplateIdAfterTemplateName(Template, Id.StartLocation, |
| 830 | &SS, true, LAngleLoc, |
| 831 | TemplateArgs, |
| 832 | TemplateArgIsType, |
| 833 | TemplateArgLocations, |
| 834 | RAngleLoc)) |
| 835 | return true; |
| 836 | |
| 837 | Diag(Id.StartLocation, diag::warn_operator_template_id_ignores_args) |
| 838 | << SourceRange(LAngleLoc, RAngleLoc); |
| 839 | break; |
| 840 | } |
| 841 | |
| 842 | case UnqualifiedId::IK_ConstructorName: |
| 843 | TNK = Actions.isTemplateName(CurScope, *Name, NameLoc, |
| 844 | &SS, /*ObjectType=*/0, EnteringContext, |
| 845 | Template); |
| 846 | break; |
| 847 | |
| 848 | case UnqualifiedId::IK_DestructorName: |
| 849 | TNK = Actions.isTemplateName(CurScope, *Name, NameLoc, |
| 850 | &SS, /*ObjectType=*/0, EnteringContext, |
| 851 | Template); |
| 852 | break; |
| 853 | |
| 854 | default: |
| 855 | return false; |
| 856 | } |
| 857 | |
| 858 | if (TNK == TNK_Non_template) |
| 859 | return false; |
| 860 | |
| 861 | // Parse the enclosed template argument list. |
| 862 | SourceLocation LAngleLoc, RAngleLoc; |
| 863 | TemplateArgList TemplateArgs; |
| 864 | TemplateArgIsTypeList TemplateArgIsType; |
| 865 | TemplateArgLocationList TemplateArgLocations; |
| 866 | if (ParseTemplateIdAfterTemplateName(Template, Id.StartLocation, |
| 867 | &SS, true, LAngleLoc, |
| 868 | TemplateArgs, |
| 869 | TemplateArgIsType, |
| 870 | TemplateArgLocations, |
| 871 | RAngleLoc)) |
| 872 | return true; |
| 873 | |
| 874 | if (Id.getKind() == UnqualifiedId::IK_Identifier || |
| 875 | Id.getKind() == UnqualifiedId::IK_OperatorFunctionId) { |
| 876 | // Form a parsed representation of the template-id to be stored in the |
| 877 | // UnqualifiedId. |
| 878 | TemplateIdAnnotation *TemplateId |
| 879 | = TemplateIdAnnotation::Allocate(TemplateArgs.size()); |
| 880 | |
| 881 | if (Id.getKind() == UnqualifiedId::IK_Identifier) { |
| 882 | TemplateId->Name = Id.Identifier; |
| 883 | TemplateId->TemplateNameLoc = Id.StartLocation; |
| 884 | } else { |
| 885 | // FIXME: Handle IK_OperatorFunctionId |
| 886 | } |
| 887 | |
| 888 | TemplateId->Template = Template.getAs<void*>(); |
| 889 | TemplateId->Kind = TNK; |
| 890 | TemplateId->LAngleLoc = LAngleLoc; |
| 891 | TemplateId->RAngleLoc = RAngleLoc; |
| 892 | void **Args = TemplateId->getTemplateArgs(); |
| 893 | bool *ArgIsType = TemplateId->getTemplateArgIsType(); |
| 894 | SourceLocation *ArgLocs = TemplateId->getTemplateArgLocations(); |
| 895 | for (unsigned Arg = 0, ArgEnd = TemplateArgs.size(); |
| 896 | Arg != ArgEnd; ++Arg) { |
| 897 | Args[Arg] = TemplateArgs[Arg]; |
| 898 | ArgIsType[Arg] = TemplateArgIsType[Arg]; |
| 899 | ArgLocs[Arg] = TemplateArgLocations[Arg]; |
| 900 | } |
| 901 | |
| 902 | Id.setTemplateId(TemplateId); |
| 903 | return false; |
| 904 | } |
| 905 | |
| 906 | // Bundle the template arguments together. |
| 907 | ASTTemplateArgsPtr TemplateArgsPtr(Actions, TemplateArgs.data(), |
| 908 | TemplateArgIsType.data(), |
| 909 | TemplateArgs.size()); |
| 910 | |
| 911 | // Constructor and destructor names. |
| 912 | Action::TypeResult Type |
| 913 | = Actions.ActOnTemplateIdType(Template, NameLoc, |
| 914 | LAngleLoc, TemplateArgsPtr, |
| 915 | &TemplateArgLocations[0], |
| 916 | RAngleLoc); |
| 917 | if (Type.isInvalid()) |
| 918 | return true; |
| 919 | |
| 920 | if (Id.getKind() == UnqualifiedId::IK_ConstructorName) |
| 921 | Id.setConstructorName(Type.get(), NameLoc, RAngleLoc); |
| 922 | else |
| 923 | Id.setDestructorName(Id.StartLocation, Type.get(), RAngleLoc); |
| 924 | |
| 925 | return false; |
| 926 | } |
| 927 | |
| 928 | /// \brief Parse a C++ unqualified-id (or a C identifier), which describes the |
| 929 | /// name of an entity. |
| 930 | /// |
| 931 | /// \code |
| 932 | /// unqualified-id: [C++ expr.prim.general] |
| 933 | /// identifier |
| 934 | /// operator-function-id |
| 935 | /// conversion-function-id |
| 936 | /// [C++0x] literal-operator-id [TODO] |
| 937 | /// ~ class-name |
| 938 | /// template-id |
| 939 | /// |
| 940 | /// operator-function-id: [C++ 13.5] |
| 941 | /// 'operator' operator |
| 942 | /// |
| 943 | /// operator: one of |
| 944 | /// new delete new[] delete[] |
| 945 | /// + - * / % ^ & | ~ |
| 946 | /// ! = < > += -= *= /= %= |
| 947 | /// ^= &= |= << >> >>= <<= == != |
| 948 | /// <= >= && || ++ -- , ->* -> |
| 949 | /// () [] |
| 950 | /// |
| 951 | /// conversion-function-id: [C++ 12.3.2] |
| 952 | /// operator conversion-type-id |
| 953 | /// |
| 954 | /// conversion-type-id: |
| 955 | /// type-specifier-seq conversion-declarator[opt] |
| 956 | /// |
| 957 | /// conversion-declarator: |
| 958 | /// ptr-operator conversion-declarator[opt] |
| 959 | /// \endcode |
| 960 | /// |
| 961 | /// \param The nested-name-specifier that preceded this unqualified-id. If |
| 962 | /// non-empty, then we are parsing the unqualified-id of a qualified-id. |
| 963 | /// |
| 964 | /// \param EnteringContext whether we are entering the scope of the |
| 965 | /// nested-name-specifier. |
| 966 | /// |
| 967 | /// \param AllowDestructorName whether we allow parsing of a destructor name. |
| 968 | /// |
| 969 | /// \param AllowConstructorName whether we allow parsing a constructor name. |
| 970 | /// |
| 971 | /// \param Result on a successful parse, contains the parsed unqualified-id. |
| 972 | /// |
| 973 | /// \returns true if parsing fails, false otherwise. |
| 974 | bool Parser::ParseUnqualifiedId(CXXScopeSpec &SS, bool EnteringContext, |
| 975 | bool AllowDestructorName, |
| 976 | bool AllowConstructorName, |
| 977 | UnqualifiedId &Result) { |
| 978 | // unqualified-id: |
| 979 | // identifier |
| 980 | // template-id (when it hasn't already been annotated) |
| 981 | if (Tok.is(tok::identifier)) { |
| 982 | // Consume the identifier. |
| 983 | IdentifierInfo *Id = Tok.getIdentifierInfo(); |
| 984 | SourceLocation IdLoc = ConsumeToken(); |
| 985 | |
| 986 | if (AllowConstructorName && |
| 987 | Actions.isCurrentClassName(*Id, CurScope, &SS)) { |
| 988 | // We have parsed a constructor name. |
| 989 | Result.setConstructorName(Actions.getTypeName(*Id, IdLoc, CurScope, |
| 990 | &SS, false), |
| 991 | IdLoc, IdLoc); |
| 992 | } else { |
| 993 | // We have parsed an identifier. |
| 994 | Result.setIdentifier(Id, IdLoc); |
| 995 | } |
| 996 | |
| 997 | // If the next token is a '<', we may have a template. |
| 998 | if (Tok.is(tok::less)) |
| 999 | return ParseUnqualifiedIdTemplateId(SS, Id, IdLoc, EnteringContext, |
| 1000 | Result); |
| 1001 | |
| 1002 | return false; |
| 1003 | } |
| 1004 | |
| 1005 | // unqualified-id: |
| 1006 | // template-id (already parsed and annotated) |
| 1007 | if (Tok.is(tok::annot_template_id)) { |
| 1008 | // FIXME: Could this be a constructor name??? |
| 1009 | |
| 1010 | // We have already parsed a template-id; consume the annotation token as |
| 1011 | // our unqualified-id. |
| 1012 | Result.setTemplateId( |
| 1013 | static_cast<TemplateIdAnnotation*>(Tok.getAnnotationValue())); |
| 1014 | ConsumeToken(); |
| 1015 | return false; |
| 1016 | } |
| 1017 | |
| 1018 | // unqualified-id: |
| 1019 | // operator-function-id |
| 1020 | // conversion-function-id |
| 1021 | if (Tok.is(tok::kw_operator)) { |
| 1022 | // Consume the 'operator' keyword. |
| 1023 | SourceLocation KeywordLoc = ConsumeToken(); |
| 1024 | |
| 1025 | // Determine what kind of operator name we have. |
| 1026 | unsigned SymbolIdx = 0; |
| 1027 | SourceLocation SymbolLocations[3]; |
| 1028 | OverloadedOperatorKind Op = OO_None; |
| 1029 | switch (Tok.getKind()) { |
| 1030 | case tok::kw_new: |
| 1031 | case tok::kw_delete: { |
| 1032 | bool isNew = Tok.getKind() == tok::kw_new; |
| 1033 | // Consume the 'new' or 'delete'. |
| 1034 | SymbolLocations[SymbolIdx++] = ConsumeToken(); |
| 1035 | if (Tok.is(tok::l_square)) { |
| 1036 | // Consume the '['. |
| 1037 | SourceLocation LBracketLoc = ConsumeBracket(); |
| 1038 | // Consume the ']'. |
| 1039 | SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square, |
| 1040 | LBracketLoc); |
| 1041 | if (RBracketLoc.isInvalid()) |
| 1042 | return true; |
| 1043 | |
| 1044 | SymbolLocations[SymbolIdx++] = LBracketLoc; |
| 1045 | SymbolLocations[SymbolIdx++] = RBracketLoc; |
| 1046 | Op = isNew? OO_Array_New : OO_Array_Delete; |
| 1047 | } else { |
| 1048 | Op = isNew? OO_New : OO_Delete; |
| 1049 | } |
| 1050 | break; |
| 1051 | } |
| 1052 | |
| 1053 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
| 1054 | case tok::Token: \ |
| 1055 | SymbolLocations[SymbolIdx++] = ConsumeToken(); \ |
| 1056 | Op = OO_##Name; \ |
| 1057 | break; |
| 1058 | #define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly) |
| 1059 | #include "clang/Basic/OperatorKinds.def" |
| 1060 | |
| 1061 | case tok::l_paren: { |
| 1062 | // Consume the '('. |
| 1063 | SourceLocation LParenLoc = ConsumeParen(); |
| 1064 | // Consume the ')'. |
| 1065 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, |
| 1066 | LParenLoc); |
| 1067 | if (RParenLoc.isInvalid()) |
| 1068 | return true; |
| 1069 | |
| 1070 | SymbolLocations[SymbolIdx++] = LParenLoc; |
| 1071 | SymbolLocations[SymbolIdx++] = RParenLoc; |
| 1072 | Op = OO_Call; |
| 1073 | break; |
| 1074 | } |
| 1075 | |
| 1076 | case tok::l_square: { |
| 1077 | // Consume the '['. |
| 1078 | SourceLocation LBracketLoc = ConsumeBracket(); |
| 1079 | // Consume the ']'. |
| 1080 | SourceLocation RBracketLoc = MatchRHSPunctuation(tok::r_square, |
| 1081 | LBracketLoc); |
| 1082 | if (RBracketLoc.isInvalid()) |
| 1083 | return true; |
| 1084 | |
| 1085 | SymbolLocations[SymbolIdx++] = LBracketLoc; |
| 1086 | SymbolLocations[SymbolIdx++] = RBracketLoc; |
| 1087 | Op = OO_Subscript; |
| 1088 | break; |
| 1089 | } |
| 1090 | |
| 1091 | case tok::code_completion: { |
| 1092 | // Code completion for the operator name. |
| 1093 | Actions.CodeCompleteOperatorName(CurScope); |
| 1094 | |
| 1095 | // Consume the operator token. |
| 1096 | ConsumeToken(); |
| 1097 | |
| 1098 | // Don't try to parse any further. |
| 1099 | return true; |
| 1100 | } |
| 1101 | |
| 1102 | default: |
| 1103 | break; |
| 1104 | } |
| 1105 | |
| 1106 | if (Op != OO_None) { |
| 1107 | // We have parsed an operator-function-id. |
| 1108 | Result.setOperatorFunctionId(KeywordLoc, Op, SymbolLocations); |
| 1109 | |
| 1110 | // If the next token is a '<', we may have a template. |
| 1111 | if (Tok.is(tok::less)) |
| 1112 | return ParseUnqualifiedIdTemplateId(SS, 0, SourceLocation(), |
| 1113 | EnteringContext, Result); |
| 1114 | |
| 1115 | return false; |
| 1116 | } |
| 1117 | |
| 1118 | // Parse a conversion-function-id. |
| 1119 | // |
| 1120 | // conversion-function-id: [C++ 12.3.2] |
| 1121 | // operator conversion-type-id |
| 1122 | // |
| 1123 | // conversion-type-id: |
| 1124 | // type-specifier-seq conversion-declarator[opt] |
| 1125 | // |
| 1126 | // conversion-declarator: |
| 1127 | // ptr-operator conversion-declarator[opt] |
| 1128 | |
| 1129 | // Parse the type-specifier-seq. |
| 1130 | DeclSpec DS; |
| 1131 | if (ParseCXXTypeSpecifierSeq(DS)) |
| 1132 | return true; |
| 1133 | |
| 1134 | // Parse the conversion-declarator, which is merely a sequence of |
| 1135 | // ptr-operators. |
| 1136 | Declarator D(DS, Declarator::TypeNameContext); |
| 1137 | ParseDeclaratorInternal(D, /*DirectDeclParser=*/0); |
| 1138 | |
| 1139 | // Finish up the type. |
| 1140 | Action::TypeResult Ty = Actions.ActOnTypeName(CurScope, D); |
| 1141 | if (Ty.isInvalid()) |
| 1142 | return true; |
| 1143 | |
| 1144 | // Note that this is a conversion-function-id. |
| 1145 | Result.setConversionFunctionId(KeywordLoc, Ty.get(), |
| 1146 | D.getSourceRange().getEnd()); |
| 1147 | return false; |
| 1148 | } |
| 1149 | |
| 1150 | if ((AllowDestructorName || SS.isSet()) && Tok.is(tok::tilde)) { |
| 1151 | // C++ [expr.unary.op]p10: |
| 1152 | // There is an ambiguity in the unary-expression ~X(), where X is a |
| 1153 | // class-name. The ambiguity is resolved in favor of treating ~ as a |
| 1154 | // unary complement rather than treating ~X as referring to a destructor. |
| 1155 | |
| 1156 | // Parse the '~'. |
| 1157 | SourceLocation TildeLoc = ConsumeToken(); |
| 1158 | |
| 1159 | // Parse the class-name. |
| 1160 | if (Tok.isNot(tok::identifier)) { |
| 1161 | Diag(Tok, diag::err_destructor_class_name); |
| 1162 | return true; |
| 1163 | } |
| 1164 | |
| 1165 | // Parse the class-name (or template-name in a simple-template-id). |
| 1166 | IdentifierInfo *ClassName = Tok.getIdentifierInfo(); |
| 1167 | SourceLocation ClassNameLoc = ConsumeToken(); |
| 1168 | |
| 1169 | // Note that this is a destructor name. |
| 1170 | Action::TypeTy *Ty = Actions.getTypeName(*ClassName, ClassNameLoc, |
| 1171 | CurScope, &SS); |
| 1172 | if (!Ty) { |
| 1173 | Diag(ClassNameLoc, diag::err_destructor_class_name); |
| 1174 | return true; |
| 1175 | } |
| 1176 | |
| 1177 | Result.setDestructorName(TildeLoc, Ty, ClassNameLoc); |
| 1178 | |
| 1179 | if (Tok.is(tok::less)) |
| 1180 | return ParseUnqualifiedIdTemplateId(SS, ClassName, ClassNameLoc, |
| 1181 | EnteringContext, Result); |
| 1182 | |
| 1183 | return false; |
| 1184 | } |
| 1185 | |
| 1186 | Diag(Tok, diag::err_expected_unqualified_id); |
| 1187 | return true; |
| 1188 | } |
| 1189 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 1190 | /// TryParseOperatorFunctionId - Attempts to parse a C++ overloaded |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 1191 | /// operator name (C++ [over.oper]). If successful, returns the |
| 1192 | /// predefined identifier that corresponds to that overloaded |
| 1193 | /// operator. Otherwise, returns NULL and does not consume any tokens. |
| 1194 | /// |
| 1195 | /// operator-function-id: [C++ 13.5] |
| 1196 | /// 'operator' operator |
| 1197 | /// |
| 1198 | /// operator: one of |
| 1199 | /// new delete new[] delete[] |
| 1200 | /// + - * / % ^ & | ~ |
| 1201 | /// ! = < > += -= *= /= %= |
| 1202 | /// ^= &= |= << >> >>= <<= == != |
| 1203 | /// <= >= && || ++ -- , ->* -> |
| 1204 | /// () [] |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1205 | OverloadedOperatorKind |
| 1206 | Parser::TryParseOperatorFunctionId(SourceLocation *EndLoc) { |
Argyrios Kyrtzidis | 9057a81 | 2008-11-07 15:54:02 +0000 | [diff] [blame] | 1207 | assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword"); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1208 | SourceLocation Loc; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 1209 | |
| 1210 | OverloadedOperatorKind Op = OO_None; |
| 1211 | switch (NextToken().getKind()) { |
| 1212 | case tok::kw_new: |
| 1213 | ConsumeToken(); // 'operator' |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1214 | Loc = ConsumeToken(); // 'new' |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 1215 | if (Tok.is(tok::l_square)) { |
| 1216 | ConsumeBracket(); // '[' |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1217 | Loc = Tok.getLocation(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 1218 | ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']' |
| 1219 | Op = OO_Array_New; |
| 1220 | } else { |
| 1221 | Op = OO_New; |
| 1222 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1223 | if (EndLoc) |
| 1224 | *EndLoc = Loc; |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 1225 | return Op; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 1226 | |
| 1227 | case tok::kw_delete: |
| 1228 | ConsumeToken(); // 'operator' |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1229 | Loc = ConsumeToken(); // 'delete' |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 1230 | if (Tok.is(tok::l_square)) { |
| 1231 | ConsumeBracket(); // '[' |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1232 | Loc = Tok.getLocation(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 1233 | ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']' |
| 1234 | Op = OO_Array_Delete; |
| 1235 | } else { |
| 1236 | Op = OO_Delete; |
| 1237 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1238 | if (EndLoc) |
| 1239 | *EndLoc = Loc; |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 1240 | return Op; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 1241 | |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 1242 | #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 1243 | case tok::Token: Op = OO_##Name; break; |
Douglas Gregor | 02bcd4c | 2008-11-10 13:38:07 +0000 | [diff] [blame] | 1244 | #define OVERLOADED_OPERATOR_MULTI(Name,Spelling,Unary,Binary,MemberOnly) |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 1245 | #include "clang/Basic/OperatorKinds.def" |
| 1246 | |
| 1247 | case tok::l_paren: |
| 1248 | ConsumeToken(); // 'operator' |
| 1249 | ConsumeParen(); // '(' |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1250 | Loc = Tok.getLocation(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 1251 | ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); // ')' |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1252 | if (EndLoc) |
| 1253 | *EndLoc = Loc; |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 1254 | return OO_Call; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 1255 | |
| 1256 | case tok::l_square: |
| 1257 | ConsumeToken(); // 'operator' |
| 1258 | ConsumeBracket(); // '[' |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1259 | Loc = Tok.getLocation(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 1260 | ExpectAndConsume(tok::r_square, diag::err_expected_rsquare); // ']' |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1261 | if (EndLoc) |
| 1262 | *EndLoc = Loc; |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 1263 | return OO_Subscript; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 1264 | |
Douglas Gregor | ed8d322 | 2009-09-18 20:05:18 +0000 | [diff] [blame] | 1265 | case tok::code_completion: { |
| 1266 | // Code completion for the operator name. |
| 1267 | Actions.CodeCompleteOperatorName(CurScope); |
| 1268 | |
| 1269 | // Consume the 'operator' token, then replace the code-completion token |
| 1270 | // with an 'operator' token and try again. |
| 1271 | SourceLocation OperatorLoc = ConsumeToken(); |
| 1272 | Tok.setLocation(OperatorLoc); |
| 1273 | Tok.setKind(tok::kw_operator); |
| 1274 | return TryParseOperatorFunctionId(EndLoc); |
| 1275 | } |
| 1276 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 1277 | default: |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 1278 | return OO_None; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 1279 | } |
| 1280 | |
Douglas Gregor | 43c7bad | 2008-11-17 16:14:12 +0000 | [diff] [blame] | 1281 | ConsumeToken(); // 'operator' |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1282 | Loc = ConsumeAnyToken(); // the operator itself |
| 1283 | if (EndLoc) |
| 1284 | *EndLoc = Loc; |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 1285 | return Op; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 1286 | } |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1287 | |
| 1288 | /// ParseConversionFunctionId - Parse a C++ conversion-function-id, |
| 1289 | /// which expresses the name of a user-defined conversion operator |
| 1290 | /// (C++ [class.conv.fct]p1). Returns the type that this operator is |
| 1291 | /// specifying a conversion for, or NULL if there was an error. |
| 1292 | /// |
| 1293 | /// conversion-function-id: [C++ 12.3.2] |
| 1294 | /// operator conversion-type-id |
| 1295 | /// |
| 1296 | /// conversion-type-id: |
| 1297 | /// type-specifier-seq conversion-declarator[opt] |
| 1298 | /// |
| 1299 | /// conversion-declarator: |
| 1300 | /// ptr-operator conversion-declarator[opt] |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1301 | Parser::TypeTy *Parser::ParseConversionFunctionId(SourceLocation *EndLoc) { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1302 | assert(Tok.is(tok::kw_operator) && "Expected 'operator' keyword"); |
| 1303 | ConsumeToken(); // 'operator' |
| 1304 | |
| 1305 | // Parse the type-specifier-seq. |
| 1306 | DeclSpec DS; |
| 1307 | if (ParseCXXTypeSpecifierSeq(DS)) |
| 1308 | return 0; |
| 1309 | |
| 1310 | // Parse the conversion-declarator, which is merely a sequence of |
| 1311 | // ptr-operators. |
| 1312 | Declarator D(DS, Declarator::TypeNameContext); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1313 | ParseDeclaratorInternal(D, /*DirectDeclParser=*/0); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1314 | if (EndLoc) |
| 1315 | *EndLoc = D.getSourceRange().getEnd(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1316 | |
| 1317 | // Finish up the type. |
| 1318 | Action::TypeResult Result = Actions.ActOnTypeName(CurScope, D); |
Douglas Gregor | 5ac8aff | 2009-01-26 22:44:13 +0000 | [diff] [blame] | 1319 | if (Result.isInvalid()) |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1320 | return 0; |
| 1321 | else |
Douglas Gregor | 5ac8aff | 2009-01-26 22:44:13 +0000 | [diff] [blame] | 1322 | return Result.get(); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1323 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1324 | |
| 1325 | /// ParseCXXNewExpression - Parse a C++ new-expression. New is used to allocate |
| 1326 | /// memory in a typesafe manner and call constructors. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1327 | /// |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 1328 | /// This method is called to parse the new expression after the optional :: has |
| 1329 | /// been already parsed. If the :: was present, "UseGlobal" is true and "Start" |
| 1330 | /// is its location. Otherwise, "Start" is the location of the 'new' token. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1331 | /// |
| 1332 | /// new-expression: |
| 1333 | /// '::'[opt] 'new' new-placement[opt] new-type-id |
| 1334 | /// new-initializer[opt] |
| 1335 | /// '::'[opt] 'new' new-placement[opt] '(' type-id ')' |
| 1336 | /// new-initializer[opt] |
| 1337 | /// |
| 1338 | /// new-placement: |
| 1339 | /// '(' expression-list ')' |
| 1340 | /// |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1341 | /// new-type-id: |
| 1342 | /// type-specifier-seq new-declarator[opt] |
| 1343 | /// |
| 1344 | /// new-declarator: |
| 1345 | /// ptr-operator new-declarator[opt] |
| 1346 | /// direct-new-declarator |
| 1347 | /// |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1348 | /// new-initializer: |
| 1349 | /// '(' expression-list[opt] ')' |
| 1350 | /// [C++0x] braced-init-list [TODO] |
| 1351 | /// |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 1352 | Parser::OwningExprResult |
| 1353 | Parser::ParseCXXNewExpression(bool UseGlobal, SourceLocation Start) { |
| 1354 | assert(Tok.is(tok::kw_new) && "expected 'new' token"); |
| 1355 | ConsumeToken(); // Consume 'new' |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1356 | |
| 1357 | // A '(' now can be a new-placement or the '(' wrapping the type-id in the |
| 1358 | // second form of new-expression. It can't be a new-type-id. |
| 1359 | |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 1360 | ExprVector PlacementArgs(Actions); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1361 | SourceLocation PlacementLParen, PlacementRParen; |
| 1362 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1363 | bool ParenTypeId; |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1364 | DeclSpec DS; |
| 1365 | Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1366 | if (Tok.is(tok::l_paren)) { |
| 1367 | // If it turns out to be a placement, we change the type location. |
| 1368 | PlacementLParen = ConsumeParen(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1369 | if (ParseExpressionListOrTypeId(PlacementArgs, DeclaratorInfo)) { |
| 1370 | SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 1371 | return ExprError(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1372 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1373 | |
| 1374 | PlacementRParen = MatchRHSPunctuation(tok::r_paren, PlacementLParen); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1375 | if (PlacementRParen.isInvalid()) { |
| 1376 | SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 1377 | return ExprError(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1378 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1379 | |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1380 | if (PlacementArgs.empty()) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1381 | // Reset the placement locations. There was no placement. |
| 1382 | PlacementLParen = PlacementRParen = SourceLocation(); |
| 1383 | ParenTypeId = true; |
| 1384 | } else { |
| 1385 | // We still need the type. |
| 1386 | if (Tok.is(tok::l_paren)) { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1387 | SourceLocation LParen = ConsumeParen(); |
| 1388 | ParseSpecifierQualifierList(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1389 | DeclaratorInfo.SetSourceRange(DS.getSourceRange()); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1390 | ParseDeclarator(DeclaratorInfo); |
| 1391 | MatchRHSPunctuation(tok::r_paren, LParen); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1392 | ParenTypeId = true; |
| 1393 | } else { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1394 | if (ParseCXXTypeSpecifierSeq(DS)) |
| 1395 | DeclaratorInfo.setInvalidType(true); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1396 | else { |
| 1397 | DeclaratorInfo.SetSourceRange(DS.getSourceRange()); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1398 | ParseDeclaratorInternal(DeclaratorInfo, |
| 1399 | &Parser::ParseDirectNewDeclarator); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1400 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1401 | ParenTypeId = false; |
| 1402 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1403 | } |
| 1404 | } else { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1405 | // A new-type-id is a simplified type-id, where essentially the |
| 1406 | // direct-declarator is replaced by a direct-new-declarator. |
| 1407 | if (ParseCXXTypeSpecifierSeq(DS)) |
| 1408 | DeclaratorInfo.setInvalidType(true); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1409 | else { |
| 1410 | DeclaratorInfo.SetSourceRange(DS.getSourceRange()); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1411 | ParseDeclaratorInternal(DeclaratorInfo, |
| 1412 | &Parser::ParseDirectNewDeclarator); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1413 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1414 | ParenTypeId = false; |
| 1415 | } |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1416 | if (DeclaratorInfo.isInvalidType()) { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1417 | SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 1418 | return ExprError(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1419 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1420 | |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 1421 | ExprVector ConstructorArgs(Actions); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1422 | SourceLocation ConstructorLParen, ConstructorRParen; |
| 1423 | |
| 1424 | if (Tok.is(tok::l_paren)) { |
| 1425 | ConstructorLParen = ConsumeParen(); |
| 1426 | if (Tok.isNot(tok::r_paren)) { |
| 1427 | CommaLocsTy CommaLocs; |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1428 | if (ParseExpressionList(ConstructorArgs, CommaLocs)) { |
| 1429 | SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 1430 | return ExprError(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1431 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1432 | } |
| 1433 | ConstructorRParen = MatchRHSPunctuation(tok::r_paren, ConstructorLParen); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1434 | if (ConstructorRParen.isInvalid()) { |
| 1435 | SkipUntil(tok::semi, /*StopAtSemi=*/true, /*DontConsume=*/true); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 1436 | return ExprError(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1437 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1440 | return Actions.ActOnCXXNew(Start, UseGlobal, PlacementLParen, |
| 1441 | move_arg(PlacementArgs), PlacementRParen, |
| 1442 | ParenTypeId, DeclaratorInfo, ConstructorLParen, |
| 1443 | move_arg(ConstructorArgs), ConstructorRParen); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1446 | /// ParseDirectNewDeclarator - Parses a direct-new-declarator. Intended to be |
| 1447 | /// passed to ParseDeclaratorInternal. |
| 1448 | /// |
| 1449 | /// direct-new-declarator: |
| 1450 | /// '[' expression ']' |
| 1451 | /// direct-new-declarator '[' constant-expression ']' |
| 1452 | /// |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 1453 | void Parser::ParseDirectNewDeclarator(Declarator &D) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1454 | // Parse the array dimensions. |
| 1455 | bool first = true; |
| 1456 | while (Tok.is(tok::l_square)) { |
| 1457 | SourceLocation LLoc = ConsumeBracket(); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 1458 | OwningExprResult Size(first ? ParseExpression() |
| 1459 | : ParseConstantExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1460 | if (Size.isInvalid()) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1461 | // Recover |
| 1462 | SkipUntil(tok::r_square); |
| 1463 | return; |
| 1464 | } |
| 1465 | first = false; |
| 1466 | |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1467 | SourceLocation RLoc = MatchRHSPunctuation(tok::r_square, LLoc); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1468 | D.AddTypeInfo(DeclaratorChunk::getArray(0, /*static=*/false, /*star=*/false, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1469 | Size.release(), LLoc, RLoc), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1470 | RLoc); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1471 | |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1472 | if (RLoc.isInvalid()) |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1473 | return; |
| 1474 | } |
| 1475 | } |
| 1476 | |
| 1477 | /// ParseExpressionListOrTypeId - Parse either an expression-list or a type-id. |
| 1478 | /// This ambiguity appears in the syntax of the C++ new operator. |
| 1479 | /// |
| 1480 | /// new-expression: |
| 1481 | /// '::'[opt] 'new' new-placement[opt] '(' type-id ')' |
| 1482 | /// new-initializer[opt] |
| 1483 | /// |
| 1484 | /// new-placement: |
| 1485 | /// '(' expression-list ')' |
| 1486 | /// |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1487 | bool Parser::ParseExpressionListOrTypeId(ExprListTy &PlacementArgs, |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 1488 | Declarator &D) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1489 | // The '(' was already consumed. |
| 1490 | if (isTypeIdInParens()) { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1491 | ParseSpecifierQualifierList(D.getMutableDeclSpec()); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1492 | D.SetSourceRange(D.getDeclSpec().getSourceRange()); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1493 | ParseDeclarator(D); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 1494 | return D.isInvalidType(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1495 | } |
| 1496 | |
| 1497 | // It's not a type, it has to be an expression list. |
| 1498 | // Discard the comma locations - ActOnCXXNew has enough parameters. |
| 1499 | CommaLocsTy CommaLocs; |
| 1500 | return ParseExpressionList(PlacementArgs, CommaLocs); |
| 1501 | } |
| 1502 | |
| 1503 | /// ParseCXXDeleteExpression - Parse a C++ delete-expression. Delete is used |
| 1504 | /// to free memory allocated by new. |
| 1505 | /// |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 1506 | /// This method is called to parse the 'delete' expression after the optional |
| 1507 | /// '::' has been already parsed. If the '::' was present, "UseGlobal" is true |
| 1508 | /// and "Start" is its location. Otherwise, "Start" is the location of the |
| 1509 | /// 'delete' token. |
| 1510 | /// |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1511 | /// delete-expression: |
| 1512 | /// '::'[opt] 'delete' cast-expression |
| 1513 | /// '::'[opt] 'delete' '[' ']' cast-expression |
Chris Lattner | 59232d3 | 2009-01-04 21:25:24 +0000 | [diff] [blame] | 1514 | Parser::OwningExprResult |
| 1515 | Parser::ParseCXXDeleteExpression(bool UseGlobal, SourceLocation Start) { |
| 1516 | assert(Tok.is(tok::kw_delete) && "Expected 'delete' keyword"); |
| 1517 | ConsumeToken(); // Consume 'delete' |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1518 | |
| 1519 | // Array delete? |
| 1520 | bool ArrayDelete = false; |
| 1521 | if (Tok.is(tok::l_square)) { |
| 1522 | ArrayDelete = true; |
| 1523 | SourceLocation LHS = ConsumeBracket(); |
| 1524 | SourceLocation RHS = MatchRHSPunctuation(tok::r_square, LHS); |
| 1525 | if (RHS.isInvalid()) |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 1526 | return ExprError(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 1529 | OwningExprResult Operand(ParseCastExpression(false)); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1530 | if (Operand.isInvalid()) |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 1531 | return move(Operand); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1532 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1533 | return Actions.ActOnCXXDelete(Start, UseGlobal, ArrayDelete, move(Operand)); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1534 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1535 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1536 | static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) { |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1537 | switch(kind) { |
| 1538 | default: assert(false && "Not a known unary type trait."); |
| 1539 | case tok::kw___has_nothrow_assign: return UTT_HasNothrowAssign; |
| 1540 | case tok::kw___has_nothrow_copy: return UTT_HasNothrowCopy; |
| 1541 | case tok::kw___has_nothrow_constructor: return UTT_HasNothrowConstructor; |
| 1542 | case tok::kw___has_trivial_assign: return UTT_HasTrivialAssign; |
| 1543 | case tok::kw___has_trivial_copy: return UTT_HasTrivialCopy; |
| 1544 | case tok::kw___has_trivial_constructor: return UTT_HasTrivialConstructor; |
| 1545 | case tok::kw___has_trivial_destructor: return UTT_HasTrivialDestructor; |
| 1546 | case tok::kw___has_virtual_destructor: return UTT_HasVirtualDestructor; |
| 1547 | case tok::kw___is_abstract: return UTT_IsAbstract; |
| 1548 | case tok::kw___is_class: return UTT_IsClass; |
| 1549 | case tok::kw___is_empty: return UTT_IsEmpty; |
| 1550 | case tok::kw___is_enum: return UTT_IsEnum; |
| 1551 | case tok::kw___is_pod: return UTT_IsPOD; |
| 1552 | case tok::kw___is_polymorphic: return UTT_IsPolymorphic; |
| 1553 | case tok::kw___is_union: return UTT_IsUnion; |
| 1554 | } |
| 1555 | } |
| 1556 | |
| 1557 | /// ParseUnaryTypeTrait - Parse the built-in unary type-trait |
| 1558 | /// pseudo-functions that allow implementation of the TR1/C++0x type traits |
| 1559 | /// templates. |
| 1560 | /// |
| 1561 | /// primary-expression: |
| 1562 | /// [GNU] unary-type-trait '(' type-id ')' |
| 1563 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1564 | Parser::OwningExprResult Parser::ParseUnaryTypeTrait() { |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1565 | UnaryTypeTrait UTT = UnaryTypeTraitFromTokKind(Tok.getKind()); |
| 1566 | SourceLocation Loc = ConsumeToken(); |
| 1567 | |
| 1568 | SourceLocation LParen = Tok.getLocation(); |
| 1569 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen)) |
| 1570 | return ExprError(); |
| 1571 | |
| 1572 | // FIXME: Error reporting absolutely sucks! If the this fails to parse a type |
| 1573 | // there will be cryptic errors about mismatched parentheses and missing |
| 1574 | // specifiers. |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 1575 | TypeResult Ty = ParseTypeName(); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1576 | |
| 1577 | SourceLocation RParen = MatchRHSPunctuation(tok::r_paren, LParen); |
| 1578 | |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 1579 | if (Ty.isInvalid()) |
| 1580 | return ExprError(); |
| 1581 | |
| 1582 | return Actions.ActOnUnaryTypeTrait(UTT, Loc, LParen, Ty.get(), RParen); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1583 | } |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1584 | |
| 1585 | /// ParseCXXAmbiguousParenExpression - We have parsed the left paren of a |
| 1586 | /// parenthesized ambiguous type-id. This uses tentative parsing to disambiguate |
| 1587 | /// based on the context past the parens. |
| 1588 | Parser::OwningExprResult |
| 1589 | Parser::ParseCXXAmbiguousParenExpression(ParenParseOption &ExprType, |
| 1590 | TypeTy *&CastTy, |
| 1591 | SourceLocation LParenLoc, |
| 1592 | SourceLocation &RParenLoc) { |
| 1593 | assert(getLang().CPlusPlus && "Should only be called for C++!"); |
| 1594 | assert(ExprType == CastExpr && "Compound literals are not ambiguous!"); |
| 1595 | assert(isTypeIdInParens() && "Not a type-id!"); |
| 1596 | |
| 1597 | OwningExprResult Result(Actions, true); |
| 1598 | CastTy = 0; |
| 1599 | |
| 1600 | // We need to disambiguate a very ugly part of the C++ syntax: |
| 1601 | // |
| 1602 | // (T())x; - type-id |
| 1603 | // (T())*x; - type-id |
| 1604 | // (T())/x; - expression |
| 1605 | // (T()); - expression |
| 1606 | // |
| 1607 | // The bad news is that we cannot use the specialized tentative parser, since |
| 1608 | // it can only verify that the thing inside the parens can be parsed as |
| 1609 | // type-id, it is not useful for determining the context past the parens. |
| 1610 | // |
| 1611 | // The good news is that the parser can disambiguate this part without |
Argyrios Kyrtzidis | a558a89 | 2009-05-22 15:12:46 +0000 | [diff] [blame] | 1612 | // making any unnecessary Action calls. |
Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 1613 | // |
| 1614 | // It uses a scheme similar to parsing inline methods. The parenthesized |
| 1615 | // tokens are cached, the context that follows is determined (possibly by |
| 1616 | // parsing a cast-expression), and then we re-introduce the cached tokens |
| 1617 | // into the token stream and parse them appropriately. |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1618 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1619 | ParenParseOption ParseAs; |
Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 1620 | CachedTokens Toks; |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1621 | |
Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 1622 | // Store the tokens of the parentheses. We will parse them after we determine |
| 1623 | // the context that follows them. |
| 1624 | if (!ConsumeAndStoreUntil(tok::r_paren, tok::unknown, Toks, tok::semi)) { |
| 1625 | // We didn't find the ')' we expected. |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1626 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 1627 | return ExprError(); |
| 1628 | } |
| 1629 | |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1630 | if (Tok.is(tok::l_brace)) { |
Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 1631 | ParseAs = CompoundLiteral; |
| 1632 | } else { |
| 1633 | bool NotCastExpr; |
Eli Friedman | b53f08a | 2009-05-25 19:41:42 +0000 | [diff] [blame] | 1634 | // FIXME: Special-case ++ and --: "(S())++;" is not a cast-expression |
| 1635 | if (Tok.is(tok::l_paren) && NextToken().is(tok::r_paren)) { |
| 1636 | NotCastExpr = true; |
| 1637 | } else { |
| 1638 | // Try parsing the cast-expression that may follow. |
| 1639 | // If it is not a cast-expression, NotCastExpr will be true and no token |
| 1640 | // will be consumed. |
| 1641 | Result = ParseCastExpression(false/*isUnaryExpression*/, |
| 1642 | false/*isAddressofOperand*/, |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1643 | NotCastExpr, false); |
Eli Friedman | b53f08a | 2009-05-25 19:41:42 +0000 | [diff] [blame] | 1644 | } |
Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 1645 | |
| 1646 | // If we parsed a cast-expression, it's really a type-id, otherwise it's |
| 1647 | // an expression. |
| 1648 | ParseAs = NotCastExpr ? SimpleExpr : CastExpr; |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1649 | } |
| 1650 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1651 | // The current token should go after the cached tokens. |
Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 1652 | Toks.push_back(Tok); |
| 1653 | // Re-enter the stored parenthesized tokens into the token stream, so we may |
| 1654 | // parse them now. |
| 1655 | PP.EnterTokenStream(Toks.data(), Toks.size(), |
| 1656 | true/*DisableMacroExpansion*/, false/*OwnsTokens*/); |
| 1657 | // Drop the current token and bring the first cached one. It's the same token |
| 1658 | // as when we entered this function. |
| 1659 | ConsumeAnyToken(); |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1660 | |
Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 1661 | if (ParseAs >= CompoundLiteral) { |
| 1662 | TypeResult Ty = ParseTypeName(); |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1663 | |
Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 1664 | // Match the ')'. |
| 1665 | if (Tok.is(tok::r_paren)) |
| 1666 | RParenLoc = ConsumeParen(); |
| 1667 | else |
| 1668 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1669 | |
Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 1670 | if (ParseAs == CompoundLiteral) { |
| 1671 | ExprType = CompoundLiteral; |
| 1672 | return ParseCompoundLiteralExpression(Ty.get(), LParenLoc, RParenLoc); |
| 1673 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1674 | |
Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 1675 | // We parsed '(' type-id ')' and the thing after it wasn't a '{'. |
| 1676 | assert(ParseAs == CastExpr); |
| 1677 | |
| 1678 | if (Ty.isInvalid()) |
| 1679 | return ExprError(); |
| 1680 | |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1681 | CastTy = Ty.get(); |
Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 1682 | |
| 1683 | // Result is what ParseCastExpression returned earlier. |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1684 | if (!Result.isInvalid()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1685 | Result = Actions.ActOnCastExpr(CurScope, LParenLoc, CastTy, RParenLoc, |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1686 | move(Result)); |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1687 | return move(Result); |
| 1688 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1689 | |
Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 1690 | // Not a compound literal, and not followed by a cast-expression. |
| 1691 | assert(ParseAs == SimpleExpr); |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1692 | |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1693 | ExprType = SimpleExpr; |
Argyrios Kyrtzidis | f40882a | 2009-05-22 21:09:47 +0000 | [diff] [blame] | 1694 | Result = ParseExpression(); |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1695 | if (!Result.isInvalid() && Tok.is(tok::r_paren)) |
| 1696 | Result = Actions.ActOnParenExpr(LParenLoc, Tok.getLocation(), move(Result)); |
| 1697 | |
| 1698 | // Match the ')'. |
| 1699 | if (Result.isInvalid()) { |
| 1700 | SkipUntil(tok::r_paren); |
| 1701 | return ExprError(); |
| 1702 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1703 | |
Argyrios Kyrtzidis | f58f45e | 2009-05-22 10:24:42 +0000 | [diff] [blame] | 1704 | if (Tok.is(tok::r_paren)) |
| 1705 | RParenLoc = ConsumeParen(); |
| 1706 | else |
| 1707 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 1708 | |
| 1709 | return move(Result); |
| 1710 | } |