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