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