| 1 | //===--- ParseCXXInlineMethods.cpp - C++ class inline methods parsing------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements parsing for C++ class inline methods. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "clang/AST/DeclTemplate.h" |
| 14 | #include "clang/Basic/DiagnosticParse.h" |
| 15 | #include "clang/Basic/DiagnosticSema.h" |
| 16 | #include "clang/Parse/Parser.h" |
| 17 | #include "clang/Parse/RAIIObjectsForParser.h" |
| 18 | #include "clang/Sema/DeclSpec.h" |
| 19 | #include "clang/Sema/EnterExpressionEvaluationContext.h" |
| 20 | #include "clang/Sema/Lookup.h" |
| 21 | #include "clang/Sema/Scope.h" |
| 22 | #include "llvm/ADT/ScopeExit.h" |
| 23 | |
| 24 | using namespace clang; |
| 25 | |
| 26 | StringLiteral *Parser::ParseCXXDeletedFunctionMessage() { |
| 27 | if (!Tok.is(K: tok::l_paren)) |
| 28 | return nullptr; |
| 29 | StringLiteral *Message = nullptr; |
| 30 | BalancedDelimiterTracker BT{*this, tok::l_paren}; |
| 31 | BT.consumeOpen(); |
| 32 | |
| 33 | if (isTokenStringLiteral()) { |
| 34 | ExprResult Res = ParseUnevaluatedStringLiteralExpression(); |
| 35 | if (Res.isUsable()) { |
| 36 | Message = Res.getAs<StringLiteral>(); |
| 37 | DiagCompat(Loc: Message->getBeginLoc(), CompatDiagId: diag_compat::delete_with_message) |
| 38 | << Message->getSourceRange(); |
| 39 | } |
| 40 | } else { |
| 41 | Diag(Loc: Tok.getLocation(), DiagID: diag::err_expected_string_literal) |
| 42 | << /*Source='in'*/ 0 << "'delete'" ; |
| 43 | SkipUntil(T: tok::r_paren, Flags: StopAtSemi | StopBeforeMatch); |
| 44 | } |
| 45 | |
| 46 | BT.consumeClose(); |
| 47 | return Message; |
| 48 | } |
| 49 | |
| 50 | void Parser::SkipDeletedFunctionBody() { |
| 51 | if (!Tok.is(K: tok::l_paren)) |
| 52 | return; |
| 53 | |
| 54 | BalancedDelimiterTracker BT{*this, tok::l_paren}; |
| 55 | BT.consumeOpen(); |
| 56 | |
| 57 | // Just skip to the end of the current declaration. |
| 58 | SkipUntil(T1: tok::r_paren, T2: tok::comma, Flags: StopAtSemi | StopBeforeMatch); |
| 59 | if (Tok.is(K: tok::r_paren)) |
| 60 | BT.consumeClose(); |
| 61 | } |
| 62 | |
| 63 | NamedDecl *Parser::ParseCXXInlineMethodDef( |
| 64 | AccessSpecifier AS, const ParsedAttributesView &AccessAttrs, |
| 65 | ParsingDeclarator &D, const ParsedTemplateInfo &TemplateInfo, |
| 66 | const VirtSpecifiers &VS, SourceLocation PureSpecLoc) { |
| 67 | assert(D.isFunctionDeclarator() && "This isn't a function declarator!" ); |
| 68 | assert(Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try, tok::equal) && |
| 69 | "Current token not a '{', ':', '=', or 'try'!" ); |
| 70 | |
| 71 | MultiTemplateParamsArg TemplateParams( |
| 72 | TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data() |
| 73 | : nullptr, |
| 74 | TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0); |
| 75 | |
| 76 | NamedDecl *FnD; |
| 77 | if (D.getDeclSpec().isFriendSpecified()) |
| 78 | FnD = Actions.ActOnFriendFunctionDecl(S: getCurScope(), D, |
| 79 | TemplateParams); |
| 80 | else { |
| 81 | FnD = Actions.ActOnCXXMemberDeclarator(S: getCurScope(), AS, D, |
| 82 | TemplateParameterLists: TemplateParams, BitfieldWidth: nullptr, |
| 83 | VS, InitStyle: ICIS_NoInit); |
| 84 | if (FnD) { |
| 85 | Actions.ProcessDeclAttributeList(S: getCurScope(), D: FnD, AttrList: AccessAttrs); |
| 86 | if (PureSpecLoc.isValid()) |
| 87 | Actions.ActOnPureSpecifier(D: FnD, PureSpecLoc); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | if (FnD) |
| 92 | HandleMemberFunctionDeclDelays(DeclaratorInfo&: D, ThisDecl: FnD); |
| 93 | |
| 94 | D.complete(D: FnD); |
| 95 | |
| 96 | if (TryConsumeToken(Expected: tok::equal)) { |
| 97 | if (!FnD) { |
| 98 | SkipUntil(T: tok::semi); |
| 99 | return nullptr; |
| 100 | } |
| 101 | |
| 102 | bool Delete = false; |
| 103 | SourceLocation KWLoc; |
| 104 | if (TryConsumeToken(Expected: tok::kw_delete, Loc&: KWLoc)) { |
| 105 | DiagCompat(Loc: KWLoc, CompatDiagId: diag_compat::defaulted_deleted_function) |
| 106 | << 1 /* deleted */; |
| 107 | StringLiteral *Message = ParseCXXDeletedFunctionMessage(); |
| 108 | Actions.SetDeclDeleted(dcl: FnD, DelLoc: KWLoc, Message); |
| 109 | Delete = true; |
| 110 | if (auto *DeclAsFunction = dyn_cast<FunctionDecl>(Val: FnD)) { |
| 111 | DeclAsFunction->setRangeEnd(PrevTokLocation); |
| 112 | } |
| 113 | } else if (TryConsumeToken(Expected: tok::kw_default, Loc&: KWLoc)) { |
| 114 | DiagCompat(Loc: KWLoc, CompatDiagId: diag_compat::defaulted_deleted_function) |
| 115 | << 0 /* defaulted */; |
| 116 | Actions.SetDeclDefaulted(dcl: FnD, DefaultLoc: KWLoc); |
| 117 | if (auto *DeclAsFunction = dyn_cast<FunctionDecl>(Val: FnD)) { |
| 118 | DeclAsFunction->setRangeEnd(PrevTokLocation); |
| 119 | } |
| 120 | } else { |
| 121 | llvm_unreachable("function definition after = not 'delete' or 'default'" ); |
| 122 | } |
| 123 | |
| 124 | if (Tok.is(K: tok::comma)) { |
| 125 | Diag(Loc: KWLoc, DiagID: diag::err_default_delete_in_multiple_declaration) |
| 126 | << Delete; |
| 127 | SkipUntil(T: tok::semi); |
| 128 | } else if (ExpectAndConsume(ExpectedTok: tok::semi, Diag: diag::err_expected_after, |
| 129 | DiagMsg: Delete ? "delete" : "default" ) && |
| 130 | !isLikelyAtStartOfNewDeclaration()) { |
| 131 | SkipUntil(T: tok::semi); |
| 132 | } |
| 133 | |
| 134 | return FnD; |
| 135 | } |
| 136 | |
| 137 | if (SkipFunctionBodies && (!FnD || Actions.canSkipFunctionBody(D: FnD)) && |
| 138 | trySkippingFunctionBody()) { |
| 139 | Actions.ActOnSkippedFunctionBody(Decl: FnD); |
| 140 | return FnD; |
| 141 | } |
| 142 | |
| 143 | // In delayed template parsing mode, if we are within a class template |
| 144 | // or if we are about to parse function member template then consume |
| 145 | // the tokens and store them for parsing at the end of the translation unit. |
| 146 | if (getLangOpts().DelayedTemplateParsing && |
| 147 | D.getFunctionDefinitionKind() == FunctionDefinitionKind::Definition && |
| 148 | !D.getDeclSpec().hasConstexprSpecifier() && |
| 149 | !(FnD && FnD->getAsFunction() && |
| 150 | FnD->getAsFunction()->getReturnType()->getContainedAutoType()) && |
| 151 | ((Actions.CurContext->isDependentContext() || |
| 152 | (TemplateInfo.Kind != ParsedTemplateKind::NonTemplate && |
| 153 | TemplateInfo.Kind != ParsedTemplateKind::ExplicitSpecialization)) && |
| 154 | !Actions.IsInsideALocalClassWithinATemplateFunction())) { |
| 155 | |
| 156 | CachedTokens Toks; |
| 157 | LexTemplateFunctionForLateParsing(Toks); |
| 158 | |
| 159 | if (FnD) { |
| 160 | FunctionDecl *FD = FnD->getAsFunction(); |
| 161 | Actions.CheckForFunctionRedefinition(FD); |
| 162 | Actions.MarkAsLateParsedTemplate(FD, FnD, Toks); |
| 163 | } |
| 164 | |
| 165 | return FnD; |
| 166 | } |
| 167 | |
| 168 | // Consume the tokens and store them for later parsing. |
| 169 | |
| 170 | LexedMethod* LM = new LexedMethod(this, FnD); |
| 171 | getCurrentClass().LateParsedDeclarations.push_back(Elt: LM); |
| 172 | CachedTokens &Toks = LM->Toks; |
| 173 | |
| 174 | tok::TokenKind kind = Tok.getKind(); |
| 175 | // Consume everything up to (and including) the left brace of the |
| 176 | // function body. |
| 177 | if (ConsumeAndStoreFunctionPrologue(Toks)) { |
| 178 | // We didn't find the left-brace we expected after the |
| 179 | // constructor initializer. |
| 180 | |
| 181 | // If we're code-completing and the completion point was in the broken |
| 182 | // initializer, we want to parse it even though that will fail. |
| 183 | if (PP.isCodeCompletionEnabled() && |
| 184 | llvm::any_of(Range&: Toks, P: [](const Token &Tok) { |
| 185 | return Tok.is(K: tok::code_completion); |
| 186 | })) { |
| 187 | // If we gave up at the completion point, the initializer list was |
| 188 | // likely truncated, so don't eat more tokens. We'll hit some extra |
| 189 | // errors, but they should be ignored in code completion. |
| 190 | return FnD; |
| 191 | } |
| 192 | |
| 193 | // We already printed an error, and it's likely impossible to recover, |
| 194 | // so don't try to parse this method later. |
| 195 | // Skip over the rest of the decl and back to somewhere that looks |
| 196 | // reasonable. |
| 197 | SkipMalformedDecl(); |
| 198 | delete getCurrentClass().LateParsedDeclarations.back(); |
| 199 | getCurrentClass().LateParsedDeclarations.pop_back(); |
| 200 | return FnD; |
| 201 | } else { |
| 202 | // Consume everything up to (and including) the matching right brace. |
| 203 | ConsumeAndStoreUntil(T1: tok::r_brace, Toks, /*StopAtSemi=*/false); |
| 204 | } |
| 205 | |
| 206 | // If we're in a function-try-block, we need to store all the catch blocks. |
| 207 | if (kind == tok::kw_try) { |
| 208 | while (Tok.is(K: tok::kw_catch)) { |
| 209 | ConsumeAndStoreUntil(T1: tok::l_brace, Toks, /*StopAtSemi=*/false); |
| 210 | ConsumeAndStoreUntil(T1: tok::r_brace, Toks, /*StopAtSemi=*/false); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | if (FnD) { |
| 215 | FunctionDecl *FD = FnD->getAsFunction(); |
| 216 | // Track that this function will eventually have a body; Sema needs |
| 217 | // to know this. |
| 218 | Actions.CheckForFunctionRedefinition(FD); |
| 219 | FD->setWillHaveBody(true); |
| 220 | } else { |
| 221 | // If semantic analysis could not build a function declaration, |
| 222 | // just throw away the late-parsed declaration. |
| 223 | delete getCurrentClass().LateParsedDeclarations.back(); |
| 224 | getCurrentClass().LateParsedDeclarations.pop_back(); |
| 225 | } |
| 226 | |
| 227 | return FnD; |
| 228 | } |
| 229 | |
| 230 | void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) { |
| 231 | assert(Tok.isOneOf(tok::l_brace, tok::equal) && |
| 232 | "Current token not a '{' or '='!" ); |
| 233 | |
| 234 | LateParsedMemberInitializer *MI = |
| 235 | new LateParsedMemberInitializer(this, VarD); |
| 236 | getCurrentClass().LateParsedDeclarations.push_back(Elt: MI); |
| 237 | CachedTokens &Toks = MI->Toks; |
| 238 | |
| 239 | tok::TokenKind kind = Tok.getKind(); |
| 240 | if (kind == tok::equal) { |
| 241 | Toks.push_back(Elt: Tok); |
| 242 | ConsumeToken(); |
| 243 | } |
| 244 | |
| 245 | if (kind == tok::l_brace) { |
| 246 | // Begin by storing the '{' token. |
| 247 | Toks.push_back(Elt: Tok); |
| 248 | ConsumeBrace(); |
| 249 | |
| 250 | // Consume everything up to (and including) the matching right brace. |
| 251 | ConsumeAndStoreUntil(T1: tok::r_brace, Toks, /*StopAtSemi=*/true); |
| 252 | } else { |
| 253 | // Consume everything up to (but excluding) the comma or semicolon. |
| 254 | ConsumeAndStoreInitializer(Toks, CIK: CachedInitKind::DefaultInitializer); |
| 255 | } |
| 256 | |
| 257 | // Store an artificial EOF token to ensure that we don't run off the end of |
| 258 | // the initializer when we come to parse it. |
| 259 | Token Eof = Token::createEof(Loc: Tok.getLocation(), Data: VarD); |
| 260 | Toks.push_back(Elt: Eof); |
| 261 | } |
| 262 | |
| 263 | LateParsedDeclaration::~LateParsedDeclaration() {} |
| 264 | void LateParsedDeclaration::ParseLexedMethodDeclarations() {} |
| 265 | void LateParsedDeclaration::ParseLexedMemberInitializers() {} |
| 266 | void LateParsedDeclaration::ParseLexedMethodDefs() {} |
| 267 | void LateParsedDeclaration::ParseLexedAttributes() {} |
| 268 | void LateParsedDeclaration::ParseLexedPragmas() {} |
| 269 | |
| 270 | Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C) |
| 271 | : Self(P), Class(C) {} |
| 272 | |
| 273 | Parser::LateParsedClass::~LateParsedClass() { |
| 274 | Self->DeallocateParsedClasses(Class); |
| 275 | } |
| 276 | |
| 277 | void Parser::LateParsedClass::ParseLexedMethodDeclarations() { |
| 278 | Self->ParseLexedMethodDeclarations(Class&: *Class); |
| 279 | } |
| 280 | |
| 281 | void Parser::LateParsedClass::ParseLexedMemberInitializers() { |
| 282 | Self->ParseLexedMemberInitializers(Class&: *Class); |
| 283 | } |
| 284 | |
| 285 | void Parser::LateParsedClass::ParseLexedMethodDefs() { |
| 286 | Self->ParseLexedMethodDefs(Class&: *Class); |
| 287 | } |
| 288 | |
| 289 | void Parser::LateParsedClass::ParseLexedAttributes() { |
| 290 | Self->ParseLexedAttributes(Class&: *Class); |
| 291 | } |
| 292 | |
| 293 | void Parser::LateParsedClass::ParseLexedPragmas() { |
| 294 | Self->ParseLexedPragmas(Class&: *Class); |
| 295 | } |
| 296 | |
| 297 | void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() { |
| 298 | Self->ParseLexedMethodDeclaration(LM&: *this); |
| 299 | } |
| 300 | |
| 301 | void Parser::LexedMethod::ParseLexedMethodDefs() { |
| 302 | Self->ParseLexedMethodDef(LM&: *this); |
| 303 | } |
| 304 | |
| 305 | void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() { |
| 306 | Self->ParseLexedMemberInitializer(MI&: *this); |
| 307 | } |
| 308 | |
| 309 | void LateParsedAttribute::ParseLexedAttributes() { |
| 310 | Self->ParseLexedAttribute(LPA&: *this, EnterScope: true, OnDefinition: false); |
| 311 | } |
| 312 | |
| 313 | void LateParsedTypeAttribute::ParseLexedAttributes() {} |
| 314 | |
| 315 | void Parser::LateParsedPragma::ParseLexedPragmas() { |
| 316 | Self->ParseLexedPragma(LP&: *this); |
| 317 | } |
| 318 | |
| 319 | struct Parser::ReenterTemplateScopeRAII { |
| 320 | Parser &P; |
| 321 | MultiParseScope Scopes; |
| 322 | TemplateParameterDepthRAII CurTemplateDepthTracker; |
| 323 | |
| 324 | ReenterTemplateScopeRAII(Parser &P, Decl *MaybeTemplated, bool Enter = true) |
| 325 | : P(P), Scopes(P), CurTemplateDepthTracker(P.TemplateParameterDepth) { |
| 326 | if (Enter) { |
| 327 | CurTemplateDepthTracker.addDepth( |
| 328 | D: P.ReenterTemplateScopes(S&: Scopes, D: MaybeTemplated)); |
| 329 | } |
| 330 | } |
| 331 | }; |
| 332 | |
| 333 | struct Parser::ReenterClassScopeRAII : ReenterTemplateScopeRAII { |
| 334 | ParsingClass &Class; |
| 335 | |
| 336 | ReenterClassScopeRAII(Parser &P, ParsingClass &Class) |
| 337 | : ReenterTemplateScopeRAII(P, Class.TagOrTemplate, |
| 338 | /*Enter=*/!Class.TopLevelClass), |
| 339 | Class(Class) { |
| 340 | // If this is the top-level class, we're still within its scope. |
| 341 | if (Class.TopLevelClass) |
| 342 | return; |
| 343 | |
| 344 | // Re-enter the class scope itself. |
| 345 | Scopes.Enter(ScopeFlags: Scope::ClassScope|Scope::DeclScope); |
| 346 | P.Actions.ActOnStartDelayedMemberDeclarations(S: P.getCurScope(), |
| 347 | Record: Class.TagOrTemplate); |
| 348 | } |
| 349 | ~ReenterClassScopeRAII() { |
| 350 | if (Class.TopLevelClass) |
| 351 | return; |
| 352 | |
| 353 | P.Actions.ActOnFinishDelayedMemberDeclarations(S: P.getCurScope(), |
| 354 | Record: Class.TagOrTemplate); |
| 355 | } |
| 356 | }; |
| 357 | |
| 358 | void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) { |
| 359 | ReenterClassScopeRAII InClassScope(*this, Class); |
| 360 | |
| 361 | for (LateParsedDeclaration *LateD : Class.LateParsedDeclarations) |
| 362 | LateD->ParseLexedMethodDeclarations(); |
| 363 | } |
| 364 | |
| 365 | void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) { |
| 366 | // If this is a member template, introduce the template parameter scope. |
| 367 | ReenterTemplateScopeRAII InFunctionTemplateScope(*this, LM.Method); |
| 368 | |
| 369 | // Start the delayed C++ method declaration |
| 370 | Actions.ActOnStartDelayedCXXMethodDeclaration(S: getCurScope(), Method: LM.Method); |
| 371 | |
| 372 | // Introduce the parameters into scope and parse their default |
| 373 | // arguments. |
| 374 | InFunctionTemplateScope.Scopes.Enter(ScopeFlags: Scope::FunctionPrototypeScope | |
| 375 | Scope::FunctionDeclarationScope | |
| 376 | Scope::DeclScope); |
| 377 | |
| 378 | // Delayed default arguments or exception specifications may contain lambdas, |
| 379 | // struct S { |
| 380 | // void ICE(int x, int = sizeof([x] { return x; }())); |
| 381 | // } |
| 382 | // |
| 383 | // struct X { |
| 384 | // void ICE(int val) noexcept(noexcept([val]{})); |
| 385 | // }; |
| 386 | // Lambda capture handling in tryCaptureVariable() expects an enclosing |
| 387 | // function scope in Sema's FunctionScopes stack. |
| 388 | Sema::FunctionScopeRAII PopFnContext(Actions); |
| 389 | Actions.PushFunctionScope(); |
| 390 | |
| 391 | for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; ++I) { |
| 392 | auto Param = cast<ParmVarDecl>(Val: LM.DefaultArgs[I].Param); |
| 393 | // Introduce the parameter into scope. |
| 394 | bool HasUnparsed = Param->hasUnparsedDefaultArg(); |
| 395 | Actions.ActOnDelayedCXXMethodParameter(S: getCurScope(), Param); |
| 396 | std::unique_ptr<CachedTokens> Toks = std::move(LM.DefaultArgs[I].Toks); |
| 397 | if (Toks) { |
| 398 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
| 399 | |
| 400 | // Mark the end of the default argument so that we know when to stop when |
| 401 | // we parse it later on. |
| 402 | Token LastDefaultArgToken = Toks->back(); |
| 403 | Token DefArgEnd = |
| 404 | Token::createEof(Loc: LastDefaultArgToken.getEndLoc(), Data: Param); |
| 405 | Toks->push_back(Elt: DefArgEnd); |
| 406 | |
| 407 | // Parse the default argument from its saved token stream. |
| 408 | Toks->push_back(Elt: Tok); // So that the current token doesn't get lost |
| 409 | PP.EnterTokenStream(Toks: *Toks, DisableMacroExpansion: true, /*IsReinject*/ true); |
| 410 | |
| 411 | // Consume the previously-pushed token. |
| 412 | ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); |
| 413 | |
| 414 | // Consume the '='. |
| 415 | assert(Tok.is(tok::equal) && "Default argument not starting with '='" ); |
| 416 | SourceLocation EqualLoc = ConsumeToken(); |
| 417 | |
| 418 | // The argument isn't actually potentially evaluated unless it is |
| 419 | // used. |
| 420 | EnterExpressionEvaluationContext Eval( |
| 421 | Actions, |
| 422 | Sema::ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed, Param); |
| 423 | |
| 424 | ExprResult DefArgResult; |
| 425 | if (getLangOpts().CPlusPlus11 && Tok.is(K: tok::l_brace)) { |
| 426 | Diag(Tok, DiagID: diag::compat_cxx11_generalized_initializer_lists); |
| 427 | DefArgResult = ParseBraceInitializer(); |
| 428 | } else |
| 429 | DefArgResult = ParseAssignmentExpression(); |
| 430 | if (DefArgResult.isInvalid()) { |
| 431 | Actions.ActOnParamDefaultArgumentError(param: Param, EqualLoc, |
| 432 | /*DefaultArg=*/nullptr); |
| 433 | } else { |
| 434 | if (Tok.isNot(K: tok::eof) || Tok.getEofData() != Param) { |
| 435 | // The last two tokens are the terminator and the saved value of |
| 436 | // Tok; the last token in the default argument is the one before |
| 437 | // those. |
| 438 | assert(Toks->size() >= 3 && "expected a token in default arg" ); |
| 439 | Diag(Loc: Tok.getLocation(), DiagID: diag::err_default_arg_unparsed) |
| 440 | << SourceRange(Tok.getLocation(), |
| 441 | (*Toks)[Toks->size() - 3].getLocation()); |
| 442 | } |
| 443 | Actions.ActOnParamDefaultArgument(param: Param, EqualLoc, |
| 444 | defarg: DefArgResult.get()); |
| 445 | } |
| 446 | |
| 447 | // There could be leftover tokens (e.g. because of an error). |
| 448 | // Skip through until we reach the 'end of default argument' token. |
| 449 | while (Tok.isNot(K: tok::eof)) |
| 450 | ConsumeAnyToken(); |
| 451 | |
| 452 | if (Tok.is(K: tok::eof) && Tok.getEofData() == Param) |
| 453 | ConsumeAnyToken(); |
| 454 | } else if (HasUnparsed) { |
| 455 | assert(Param->hasInheritedDefaultArg()); |
| 456 | FunctionDecl *Old; |
| 457 | if (const auto *FunTmpl = dyn_cast<FunctionTemplateDecl>(Val: LM.Method)) |
| 458 | Old = |
| 459 | cast<FunctionDecl>(Val: FunTmpl->getTemplatedDecl())->getPreviousDecl(); |
| 460 | else |
| 461 | Old = cast<FunctionDecl>(Val: LM.Method)->getPreviousDecl(); |
| 462 | if (Old) { |
| 463 | ParmVarDecl *OldParam = Old->getParamDecl(i: I); |
| 464 | assert(!OldParam->hasUnparsedDefaultArg()); |
| 465 | if (OldParam->hasUninstantiatedDefaultArg()) |
| 466 | Param->setUninstantiatedDefaultArg( |
| 467 | OldParam->getUninstantiatedDefaultArg()); |
| 468 | else |
| 469 | Param->setDefaultArg(OldParam->getInit()); |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | // Parse a delayed exception-specification, if there is one. |
| 475 | if (CachedTokens *Toks = LM.ExceptionSpecTokens) { |
| 476 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
| 477 | |
| 478 | // Add the 'stop' token. |
| 479 | Token LastExceptionSpecToken = Toks->back(); |
| 480 | Token ExceptionSpecEnd = |
| 481 | Token::createEof(Loc: LastExceptionSpecToken.getEndLoc(), Data: LM.Method); |
| 482 | Toks->push_back(Elt: ExceptionSpecEnd); |
| 483 | |
| 484 | // Parse the default argument from its saved token stream. |
| 485 | Toks->push_back(Elt: Tok); // So that the current token doesn't get lost |
| 486 | PP.EnterTokenStream(Toks: *Toks, DisableMacroExpansion: true, /*IsReinject*/true); |
| 487 | |
| 488 | // Consume the previously-pushed token. |
| 489 | ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); |
| 490 | |
| 491 | // C++11 [expr.prim.general]p3: |
| 492 | // If a declaration declares a member function or member function |
| 493 | // template of a class X, the expression this is a prvalue of type |
| 494 | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq |
| 495 | // and the end of the function-definition, member-declarator, or |
| 496 | // declarator. |
| 497 | CXXMethodDecl *Method; |
| 498 | FunctionDecl *FunctionToPush; |
| 499 | if (FunctionTemplateDecl *FunTmpl |
| 500 | = dyn_cast<FunctionTemplateDecl>(Val: LM.Method)) |
| 501 | FunctionToPush = FunTmpl->getTemplatedDecl(); |
| 502 | else |
| 503 | FunctionToPush = cast<FunctionDecl>(Val: LM.Method); |
| 504 | Method = dyn_cast<CXXMethodDecl>(Val: FunctionToPush); |
| 505 | |
| 506 | // Setup the CurScope to match the function DeclContext - we have such |
| 507 | // assumption in IsInFnTryBlockHandler(). |
| 508 | ParseScope FnScope(this, Scope::FnScope); |
| 509 | Sema::ContextRAII FnContext(Actions, FunctionToPush, |
| 510 | /*NewThisContext=*/false); |
| 511 | |
| 512 | Sema::CXXThisScopeRAII ThisScope( |
| 513 | Actions, Method ? Method->getParent() : nullptr, |
| 514 | Method ? Method->getMethodQualifiers() : Qualifiers{}, |
| 515 | Method && getLangOpts().CPlusPlus11); |
| 516 | |
| 517 | // Parse the exception-specification. |
| 518 | SourceRange SpecificationRange; |
| 519 | SmallVector<ParsedType, 4> DynamicExceptions; |
| 520 | SmallVector<SourceRange, 4> DynamicExceptionRanges; |
| 521 | ExprResult NoexceptExpr; |
| 522 | CachedTokens *ExceptionSpecTokens; |
| 523 | |
| 524 | ExceptionSpecificationType EST |
| 525 | = tryParseExceptionSpecification(/*Delayed=*/false, SpecificationRange, |
| 526 | DynamicExceptions, |
| 527 | DynamicExceptionRanges, NoexceptExpr, |
| 528 | ExceptionSpecTokens); |
| 529 | |
| 530 | if (Tok.isNot(K: tok::eof) || Tok.getEofData() != LM.Method) |
| 531 | Diag(Loc: Tok.getLocation(), DiagID: diag::err_except_spec_unparsed); |
| 532 | |
| 533 | // Attach the exception-specification to the method. |
| 534 | Actions.actOnDelayedExceptionSpecification(D: LM.Method, EST, |
| 535 | SpecificationRange, |
| 536 | DynamicExceptions, |
| 537 | DynamicExceptionRanges, |
| 538 | NoexceptExpr: NoexceptExpr.isUsable()? |
| 539 | NoexceptExpr.get() : nullptr); |
| 540 | |
| 541 | // There could be leftover tokens (e.g. because of an error). |
| 542 | // Skip through until we reach the original token position. |
| 543 | while (Tok.isNot(K: tok::eof)) |
| 544 | ConsumeAnyToken(); |
| 545 | |
| 546 | // Clean up the remaining EOF token. |
| 547 | if (Tok.is(K: tok::eof) && Tok.getEofData() == LM.Method) |
| 548 | ConsumeAnyToken(); |
| 549 | |
| 550 | delete Toks; |
| 551 | LM.ExceptionSpecTokens = nullptr; |
| 552 | } |
| 553 | |
| 554 | InFunctionTemplateScope.Scopes.Exit(); |
| 555 | |
| 556 | // Finish the delayed C++ method declaration. |
| 557 | Actions.ActOnFinishDelayedCXXMethodDeclaration(S: getCurScope(), Method: LM.Method); |
| 558 | } |
| 559 | |
| 560 | void Parser::ParseLexedMethodDefs(ParsingClass &Class) { |
| 561 | ReenterClassScopeRAII InClassScope(*this, Class); |
| 562 | |
| 563 | for (LateParsedDeclaration *D : Class.LateParsedDeclarations) |
| 564 | D->ParseLexedMethodDefs(); |
| 565 | } |
| 566 | |
| 567 | void Parser::ParseLexedMethodDef(LexedMethod &LM) { |
| 568 | // If this is a member template, introduce the template parameter scope. |
| 569 | ReenterTemplateScopeRAII InFunctionTemplateScope(*this, LM.D); |
| 570 | |
| 571 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
| 572 | |
| 573 | assert(!LM.Toks.empty() && "Empty body!" ); |
| 574 | Token LastBodyToken = LM.Toks.back(); |
| 575 | Token BodyEnd = Token::createEof(Loc: LastBodyToken.getEndLoc(), Data: LM.D); |
| 576 | LM.Toks.push_back(Elt: BodyEnd); |
| 577 | // Append the current token at the end of the new token stream so that it |
| 578 | // doesn't get lost. |
| 579 | LM.Toks.push_back(Elt: Tok); |
| 580 | PP.EnterTokenStream(Toks: LM.Toks, DisableMacroExpansion: true, /*IsReinject*/true); |
| 581 | |
| 582 | // Consume the previously pushed token. |
| 583 | ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); |
| 584 | assert(Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try) |
| 585 | && "Inline method not starting with '{', ':' or 'try'" ); |
| 586 | |
| 587 | // Parse the method body. Function body parsing code is similar enough |
| 588 | // to be re-used for method bodies as well. |
| 589 | ParseScope FnScope(this, Scope::FnScope | Scope::DeclScope | |
| 590 | Scope::CompoundStmtScope); |
| 591 | Sema::FPFeaturesStateRAII SaveFPFeatures(Actions); |
| 592 | |
| 593 | Actions.ActOnStartOfFunctionDef(S: getCurScope(), D: LM.D); |
| 594 | |
| 595 | llvm::scope_exit _([&]() { |
| 596 | while (Tok.isNot(K: tok::eof)) |
| 597 | ConsumeAnyToken(); |
| 598 | |
| 599 | if (Tok.is(K: tok::eof) && Tok.getEofData() == LM.D) |
| 600 | ConsumeAnyToken(); |
| 601 | |
| 602 | if (auto *FD = dyn_cast_or_null<FunctionDecl>(Val: LM.D)) |
| 603 | if (isa<CXXMethodDecl>(Val: FD) || |
| 604 | FD->isInIdentifierNamespace(NS: Decl::IDNS_OrdinaryFriend)) |
| 605 | Actions.ActOnFinishInlineFunctionDef(D: FD); |
| 606 | }); |
| 607 | |
| 608 | assert((Actions.getDiagnostics().hasErrorOccurred() || |
| 609 | !isa<FunctionTemplateDecl>(LM.D) || |
| 610 | cast<FunctionTemplateDecl>(LM.D)->getTemplateParameters()->getDepth() |
| 611 | < TemplateParameterDepth) && |
| 612 | "TemplateParameterDepth should be greater than the depth of " |
| 613 | "current template being instantiated!" ); |
| 614 | |
| 615 | ParseFunctionBody(D: LM.D, BodyScope&: FnScope); |
| 616 | } |
| 617 | |
| 618 | void Parser::ParseLexedMemberInitializers(ParsingClass &Class) { |
| 619 | ReenterClassScopeRAII InClassScope(*this, Class); |
| 620 | |
| 621 | if (!Class.LateParsedDeclarations.empty()) { |
| 622 | // C++11 [expr.prim.general]p4: |
| 623 | // Otherwise, if a member-declarator declares a non-static data member |
| 624 | // (9.2) of a class X, the expression this is a prvalue of type "pointer |
| 625 | // to X" within the optional brace-or-equal-initializer. It shall not |
| 626 | // appear elsewhere in the member-declarator. |
| 627 | // FIXME: This should be done in ParseLexedMemberInitializer, not here. |
| 628 | Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate, |
| 629 | Qualifiers()); |
| 630 | |
| 631 | for (LateParsedDeclaration *D : Class.LateParsedDeclarations) |
| 632 | D->ParseLexedMemberInitializers(); |
| 633 | } |
| 634 | |
| 635 | Actions.ActOnFinishDelayedMemberInitializers(Record: Class.TagOrTemplate); |
| 636 | } |
| 637 | |
| 638 | void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) { |
| 639 | if (!MI.Field || MI.Field->isInvalidDecl()) |
| 640 | return; |
| 641 | |
| 642 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
| 643 | |
| 644 | // Append the current token at the end of the new token stream so that it |
| 645 | // doesn't get lost. |
| 646 | MI.Toks.push_back(Elt: Tok); |
| 647 | PP.EnterTokenStream(Toks: MI.Toks, DisableMacroExpansion: true, /*IsReinject*/true); |
| 648 | |
| 649 | // Consume the previously pushed token. |
| 650 | ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); |
| 651 | |
| 652 | SourceLocation EqualLoc; |
| 653 | |
| 654 | Actions.ActOnStartCXXInClassMemberInitializer(); |
| 655 | |
| 656 | // The initializer isn't actually potentially evaluated unless it is |
| 657 | // used. |
| 658 | EnterExpressionEvaluationContext Eval( |
| 659 | Actions, Sema::ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed); |
| 660 | |
| 661 | ExprResult Init = ParseCXXMemberInitializer(D: MI.Field, /*IsFunction=*/false, |
| 662 | EqualLoc); |
| 663 | |
| 664 | Actions.ActOnFinishCXXInClassMemberInitializer(VarDecl: MI.Field, EqualLoc, Init); |
| 665 | |
| 666 | // The next token should be our artificial terminating EOF token. |
| 667 | if (Tok.isNot(K: tok::eof)) { |
| 668 | if (!Init.isInvalid()) { |
| 669 | SourceLocation EndLoc = PP.getLocForEndOfToken(Loc: PrevTokLocation); |
| 670 | if (!EndLoc.isValid()) |
| 671 | EndLoc = Tok.getLocation(); |
| 672 | // No fixit; we can't recover as if there were a semicolon here. |
| 673 | Diag(Loc: EndLoc, DiagID: diag::err_expected_semi_decl_list); |
| 674 | } |
| 675 | |
| 676 | // Consume tokens until we hit the artificial EOF. |
| 677 | while (Tok.isNot(K: tok::eof)) |
| 678 | ConsumeAnyToken(); |
| 679 | } |
| 680 | // Make sure this is *our* artificial EOF token. |
| 681 | if (Tok.getEofData() == MI.Field) |
| 682 | ConsumeAnyToken(); |
| 683 | } |
| 684 | |
| 685 | void Parser::ParseLexedAttributes(ParsingClass &Class) { |
| 686 | ReenterClassScopeRAII InClassScope(*this, Class); |
| 687 | |
| 688 | for (LateParsedDeclaration *LateD : Class.LateParsedDeclarations) |
| 689 | LateD->ParseLexedAttributes(); |
| 690 | } |
| 691 | |
| 692 | void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D, |
| 693 | bool EnterScope, bool OnDefinition, |
| 694 | ParsedAttributes *OutAttrs) { |
| 695 | assert(LAs.parseSoon() && |
| 696 | "Attribute list should be marked for immediate parsing." ); |
| 697 | for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) { |
| 698 | if (D) |
| 699 | LAs[i]->addDecl(D); |
| 700 | ParseLexedAttribute(LPA&: *LAs[i], EnterScope, OnDefinition, OutAttrs); |
| 701 | delete LAs[i]; |
| 702 | } |
| 703 | LAs.clear(); |
| 704 | } |
| 705 | |
| 706 | /// Collect the references to parameters in \p Attrs' arguments. |
| 707 | static void collectParamRefs(const ParsedAttributes &Attrs, |
| 708 | SmallVectorImpl<const DeclRefExpr *> &Refs) { |
| 709 | SmallVector<const Stmt *, 8> Worklist; |
| 710 | for (const ParsedAttr &AL : Attrs) |
| 711 | for (unsigned I = 0, E = AL.getNumArgs(); I != E; ++I) |
| 712 | if (AL.isArgExpr(Arg: I)) |
| 713 | Worklist.push_back(Elt: AL.getArgAsExpr(Arg: I)); |
| 714 | while (!Worklist.empty()) { |
| 715 | const Stmt *S = Worklist.pop_back_val(); |
| 716 | if (!S) |
| 717 | continue; |
| 718 | if (const auto *DRE = dyn_cast<DeclRefExpr>(Val: S); |
| 719 | DRE && isa<ParmVarDecl>(Val: DRE->getDecl())) |
| 720 | Refs.push_back(Elt: DRE); |
| 721 | llvm::append_range(C&: Worklist, R: S->children()); |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | bool Parser::checkLateAttributeParamRefs( |
| 726 | const LateParsedAttribute &LPA, const Decl *D, |
| 727 | ArrayRef<const DeclRefExpr *> ParamRefs, bool ReenteredProtoParams) { |
| 728 | auto IsPointeeParam = [&](const ParmVarDecl *PVD) { |
| 729 | return ReenteredProtoParams && llvm::is_contained(Range: LPA.ProtoParams, Element: PVD); |
| 730 | }; |
| 731 | |
| 732 | // In a C++ class, attributes were always late parsed and a pointee's |
| 733 | // parameters were not in scope, so a name that now binds to one used to bind |
| 734 | // to whatever else it names outside the prototype. Rather than silently |
| 735 | // change its meaning, reject the attribute as ambiguous. |
| 736 | if (getLangOpts().CPlusPlus && getCurScope()->isClassScope()) { |
| 737 | for (const DeclRefExpr *DRE : ParamRefs) { |
| 738 | const auto *PVD = cast<ParmVarDecl>(Val: DRE->getDecl()); |
| 739 | if (!IsPointeeParam(PVD)) |
| 740 | continue; |
| 741 | LookupResult R(Actions, PVD->getDeclName(), DRE->getLocation(), |
| 742 | Sema::LookupOrdinaryName); |
| 743 | if (!Actions.LookupName(R, S: getCurScope()) || R.isAmbiguous()) |
| 744 | continue; |
| 745 | Diag(Loc: DRE->getLocation(), DiagID: diag::err_ambiguous_reference) |
| 746 | << PVD->getDeclName(); |
| 747 | Diag(Loc: PVD->getLocation(), DiagID: diag::note_ambiguous_candidate) << PVD; |
| 748 | for (const NamedDecl *Other : R) |
| 749 | Diag(Loc: Other->getLocation(), DiagID: diag::note_ambiguous_candidate) << Other; |
| 750 | return true; |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | // A name binds the same way in a template, but instantiation cannot yet map a |
| 755 | // pointee's parameters, which are instantiated with the declaration's type |
| 756 | // and not kept, nor a parameter declared after the one the attribute is on, |
| 757 | // which is instantiated after that parameter's attributes. Reject the |
| 758 | // attribute rather than instantiate it wrongly. |
| 759 | // FIXME: Map these parameters during template instantiation. |
| 760 | if (Actions.CurContext->isDependentContext() || |
| 761 | getCurScope()->getTemplateParamParent() || |
| 762 | Actions.getCurGenericLambda()) { |
| 763 | const auto *DParam = dyn_cast_or_null<ParmVarDecl>(Val: D); |
| 764 | for (const DeclRefExpr *DRE : ParamRefs) { |
| 765 | const auto *PVD = cast<ParmVarDecl>(Val: DRE->getDecl()); |
| 766 | bool Pointee = IsPointeeParam(PVD); |
| 767 | if (!Pointee && |
| 768 | !(DParam && |
| 769 | DParam->getFunctionScopeDepth() == PVD->getFunctionScopeDepth() && |
| 770 | DParam->getFunctionScopeIndex() < PVD->getFunctionScopeIndex())) |
| 771 | continue; |
| 772 | Diag(Loc: DRE->getLocation(), DiagID: diag::err_late_attribute_param_in_template) |
| 773 | << &LPA.AttrName << !Pointee << PVD; |
| 774 | return true; |
| 775 | } |
| 776 | } |
| 777 | return false; |
| 778 | } |
| 779 | |
| 780 | void Parser::ParseLexedAttribute(LateParsedAttribute &LPA, bool EnterScope, |
| 781 | bool OnDefinition, |
| 782 | ParsedAttributes *OutAttrs) { |
| 783 | ParsedAttributes Attrs(AttrFactory); |
| 784 | |
| 785 | if (LPA.Decls.size() > 0) { |
| 786 | Decl *D = LPA.Decls[0]; |
| 787 | bool HasFuncScope = EnterScope && LPA.Decls.size() == 1 && |
| 788 | D->isFunctionOrFunctionTemplate(); |
| 789 | bool IsCPlusPlus = getLangOpts().CPlusPlus; |
| 790 | |
| 791 | NamedDecl *ND = dyn_cast<NamedDecl>(Val: D); |
| 792 | RecordDecl *RD = dyn_cast_or_null<RecordDecl>(Val: D->getDeclContext()); |
| 793 | |
| 794 | // Allow 'this' within late-parsed attributes. |
| 795 | Sema::CXXThisScopeRAII ThisScope(Actions, RD, Qualifiers(), |
| 796 | IsCPlusPlus && ND && |
| 797 | ND->isCXXInstanceMember()); |
| 798 | |
| 799 | // If the Decl is templatized, add template parameters to the scope. |
| 800 | ReenterTemplateScopeRAII InDeclScope(*this, D, IsCPlusPlus && EnterScope); |
| 801 | |
| 802 | // If the Decl is on a function, add function parameters to the scope. |
| 803 | if (HasFuncScope) { |
| 804 | InDeclScope.Scopes.Enter(ScopeFlags: Scope::FnScope | Scope::DeclScope | |
| 805 | Scope::CompoundStmtScope); |
| 806 | Actions.ActOnReenterFunctionContext(S: Actions.CurScope, D); |
| 807 | } |
| 808 | |
| 809 | // For a function pointer field or variable, the arguments may name a |
| 810 | // parameter of the pointee. Re-enter them in a prototype scope nested as it |
| 811 | // was when the attribute was written, so they shadow any outer declaration |
| 812 | // of the same name. In a C struct, ActOnIdExpression finds the struct's |
| 813 | // members only while its scope is innermost, so add the parameters to that |
| 814 | // scope instead; it lets them shadow a member of the same name. |
| 815 | bool HasProtoParams = !HasFuncScope && !LPA.ProtoParams.empty(); |
| 816 | bool InCStruct = !IsCPlusPlus && getCurScope()->isClassScope(); |
| 817 | ParseScope ProtoScope(this, |
| 818 | Scope::FunctionPrototypeScope | Scope::DeclScope, |
| 819 | HasProtoParams && !InCStruct); |
| 820 | if (HasProtoParams) |
| 821 | Actions.ActOnReenterFunctionPrototypeParams(S: Actions.getCurScope(), |
| 822 | Params: LPA.ProtoParams); |
| 823 | |
| 824 | ParsedAttributes Parsed = ParseLexedAttributeTokens(LPA); |
| 825 | |
| 826 | if (HasProtoParams && InCStruct) |
| 827 | Actions.ActOnExitFunctionPrototypeParams(S: Actions.getCurScope(), |
| 828 | Params: LPA.ProtoParams); |
| 829 | ProtoScope.Exit(); |
| 830 | |
| 831 | SmallVector<const DeclRefExpr *, 4> ParamRefs; |
| 832 | collectParamRefs(Attrs: Parsed, Refs&: ParamRefs); |
| 833 | if (!ParamRefs.empty() && |
| 834 | checkLateAttributeParamRefs(LPA, D, ParamRefs, ReenteredProtoParams: HasProtoParams)) |
| 835 | Parsed.clear(); |
| 836 | Attrs.takeAllAppendingFrom(Other&: Parsed); |
| 837 | |
| 838 | if (HasFuncScope) |
| 839 | Actions.ActOnExitFunctionContext(); |
| 840 | } else { |
| 841 | Diag(Loc: LPA.AttrNameLoc, DiagID: diag::warn_attribute_no_decl) |
| 842 | << LPA.AttrName.getName(); |
| 843 | } |
| 844 | |
| 845 | if (OnDefinition && !Attrs.empty() && !Attrs.begin()->isCXX11Attribute() && |
| 846 | Attrs.begin()->isKnownToGCC()) |
| 847 | Diag(Tok, DiagID: diag::warn_attribute_on_function_definition) << &LPA.AttrName; |
| 848 | |
| 849 | for (auto *D : LPA.Decls) |
| 850 | Actions.ActOnFinishDelayedAttribute(S: getCurScope(), D, Attrs); |
| 851 | |
| 852 | if (OutAttrs) |
| 853 | OutAttrs->takeAllAppendingFrom(Other&: Attrs); |
| 854 | } |
| 855 | |
| 856 | void Parser::ParseLexedPragmas(ParsingClass &Class) { |
| 857 | ReenterClassScopeRAII InClassScope(*this, Class); |
| 858 | |
| 859 | for (LateParsedDeclaration *D : Class.LateParsedDeclarations) |
| 860 | D->ParseLexedPragmas(); |
| 861 | } |
| 862 | |
| 863 | void Parser::ParseLexedPragma(LateParsedPragma &LP) { |
| 864 | PP.EnterToken(Tok, /*IsReinject=*/true); |
| 865 | PP.EnterTokenStream(Toks: LP.toks(), /*DisableMacroExpansion=*/true, |
| 866 | /*IsReinject=*/true); |
| 867 | |
| 868 | // Consume the previously pushed token. |
| 869 | ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); |
| 870 | assert(Tok.isAnnotation() && "Expected annotation token." ); |
| 871 | switch (Tok.getKind()) { |
| 872 | case tok::annot_attr_openmp: |
| 873 | case tok::annot_pragma_openmp: { |
| 874 | AccessSpecifier AS = LP.getAccessSpecifier(); |
| 875 | ParsedAttributes Attrs(AttrFactory); |
| 876 | (void)ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs); |
| 877 | break; |
| 878 | } |
| 879 | default: |
| 880 | llvm_unreachable("Unexpected token." ); |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | bool Parser::ConsumeAndStoreUntil(tok::TokenKind T1, tok::TokenKind T2, |
| 885 | CachedTokens &Toks, |
| 886 | bool StopAtSemi, bool ConsumeFinalToken) { |
| 887 | // We always want this function to consume at least one token if the first |
| 888 | // token isn't T and if not at EOF. |
| 889 | bool isFirstTokenConsumed = true; |
| 890 | while (true) { |
| 891 | // If we found one of the tokens, stop and return true. |
| 892 | if (Tok.is(K: T1) || Tok.is(K: T2)) { |
| 893 | if (ConsumeFinalToken) { |
| 894 | Toks.push_back(Elt: Tok); |
| 895 | ConsumeAnyToken(); |
| 896 | } |
| 897 | return true; |
| 898 | } |
| 899 | |
| 900 | switch (Tok.getKind()) { |
| 901 | case tok::eof: |
| 902 | case tok::annot_module_begin: |
| 903 | case tok::annot_module_end: |
| 904 | case tok::annot_module_include: |
| 905 | // Ran out of tokens. |
| 906 | return false; |
| 907 | |
| 908 | case tok::annot_pragma_openacc: |
| 909 | case tok::annot_pragma_openmp: |
| 910 | case tok::annot_attr_openmp: { |
| 911 | // Ignore any tokens inside of a OMP/OpenACC pragma, as these should just |
| 912 | // be taken as 1. |
| 913 | tok::TokenKind EndKind = Tok.is(K: tok::annot_pragma_openacc) |
| 914 | ? tok::annot_pragma_openacc_end |
| 915 | : tok::annot_pragma_openmp_end; |
| 916 | Toks.push_back(Elt: Tok); |
| 917 | ConsumeAnnotationToken(); |
| 918 | while (Tok.isNot(K: EndKind) && Tok.isNot(K: tok::eof)) { |
| 919 | Toks.push_back(Elt: Tok); |
| 920 | ConsumeAnyToken(); |
| 921 | } |
| 922 | if (Tok.is(K: EndKind)) { |
| 923 | Toks.push_back(Elt: Tok); |
| 924 | ConsumeAnnotationToken(); |
| 925 | } |
| 926 | break; |
| 927 | } |
| 928 | |
| 929 | case tok::l_paren: |
| 930 | // Recursively consume properly-nested parens. |
| 931 | Toks.push_back(Elt: Tok); |
| 932 | ConsumeParen(); |
| 933 | ConsumeAndStoreUntil(T1: tok::r_paren, Toks, /*StopAtSemi=*/false); |
| 934 | break; |
| 935 | case tok::l_square: |
| 936 | // Recursively consume properly-nested square brackets. |
| 937 | Toks.push_back(Elt: Tok); |
| 938 | ConsumeBracket(); |
| 939 | ConsumeAndStoreUntil(T1: tok::r_square, Toks, /*StopAtSemi=*/false); |
| 940 | break; |
| 941 | case tok::l_brace: |
| 942 | // Recursively consume properly-nested braces. |
| 943 | Toks.push_back(Elt: Tok); |
| 944 | ConsumeBrace(); |
| 945 | ConsumeAndStoreUntil(T1: tok::r_brace, Toks, /*StopAtSemi=*/false); |
| 946 | break; |
| 947 | |
| 948 | // Okay, we found a ']' or '}' or ')', which we think should be balanced. |
| 949 | // Since the user wasn't looking for this token (if they were, it would |
| 950 | // already be handled), this isn't balanced. If there is a LHS token at a |
| 951 | // higher level, we will assume that this matches the unbalanced token |
| 952 | // and return it. Otherwise, this is a spurious RHS token, which we skip. |
| 953 | case tok::r_paren: |
| 954 | if (ParenCount && !isFirstTokenConsumed) |
| 955 | return false; // Matches something. |
| 956 | Toks.push_back(Elt: Tok); |
| 957 | ConsumeParen(); |
| 958 | break; |
| 959 | case tok::r_square: |
| 960 | if (BracketCount && !isFirstTokenConsumed) |
| 961 | return false; // Matches something. |
| 962 | Toks.push_back(Elt: Tok); |
| 963 | ConsumeBracket(); |
| 964 | break; |
| 965 | case tok::r_brace: |
| 966 | if (BraceCount && !isFirstTokenConsumed) |
| 967 | return false; // Matches something. |
| 968 | Toks.push_back(Elt: Tok); |
| 969 | ConsumeBrace(); |
| 970 | break; |
| 971 | |
| 972 | case tok::semi: |
| 973 | if (StopAtSemi) |
| 974 | return false; |
| 975 | [[fallthrough]]; |
| 976 | default: |
| 977 | // consume this token. |
| 978 | Toks.push_back(Elt: Tok); |
| 979 | ConsumeAnyToken(/*ConsumeCodeCompletionTok*/true); |
| 980 | break; |
| 981 | } |
| 982 | isFirstTokenConsumed = false; |
| 983 | } |
| 984 | } |
| 985 | |
| 986 | bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) { |
| 987 | if (Tok.is(K: tok::kw_try)) { |
| 988 | Toks.push_back(Elt: Tok); |
| 989 | ConsumeToken(); |
| 990 | } |
| 991 | |
| 992 | if (Tok.isNot(K: tok::colon)) { |
| 993 | // Easy case, just a function body. |
| 994 | |
| 995 | // Grab any remaining garbage to be diagnosed later. We stop when we reach a |
| 996 | // brace: an opening one is the function body, while a closing one probably |
| 997 | // means we've reached the end of the class. |
| 998 | ConsumeAndStoreUntil(T1: tok::l_brace, T2: tok::r_brace, Toks, |
| 999 | /*StopAtSemi=*/true, |
| 1000 | /*ConsumeFinalToken=*/false); |
| 1001 | if (Tok.isNot(K: tok::l_brace)) |
| 1002 | return Diag(Loc: Tok.getLocation(), DiagID: diag::err_expected) << tok::l_brace; |
| 1003 | |
| 1004 | Toks.push_back(Elt: Tok); |
| 1005 | ConsumeBrace(); |
| 1006 | return false; |
| 1007 | } |
| 1008 | |
| 1009 | Toks.push_back(Elt: Tok); |
| 1010 | ConsumeToken(); |
| 1011 | |
| 1012 | // We can't reliably skip over a mem-initializer-id, because it could be |
| 1013 | // a template-id involving not-yet-declared names. Given: |
| 1014 | // |
| 1015 | // S ( ) : a < b < c > ( e ) |
| 1016 | // |
| 1017 | // 'e' might be an initializer or part of a template argument, depending |
| 1018 | // on whether 'b' is a template. |
| 1019 | |
| 1020 | // Track whether we might be inside a template argument. We can give |
| 1021 | // significantly better diagnostics if we know that we're not. |
| 1022 | bool MightBeTemplateArgument = false; |
| 1023 | |
| 1024 | while (true) { |
| 1025 | // Skip over the mem-initializer-id, if possible. |
| 1026 | if (Tok.is(K: tok::kw_decltype)) { |
| 1027 | Toks.push_back(Elt: Tok); |
| 1028 | SourceLocation OpenLoc = ConsumeToken(); |
| 1029 | if (Tok.isNot(K: tok::l_paren)) |
| 1030 | return Diag(Loc: Tok.getLocation(), DiagID: diag::err_expected_lparen_after) |
| 1031 | << "decltype" ; |
| 1032 | Toks.push_back(Elt: Tok); |
| 1033 | ConsumeParen(); |
| 1034 | if (!ConsumeAndStoreUntil(T1: tok::r_paren, Toks, /*StopAtSemi=*/true)) { |
| 1035 | Diag(Loc: Tok.getLocation(), DiagID: diag::err_expected) << tok::r_paren; |
| 1036 | Diag(Loc: OpenLoc, DiagID: diag::note_matching) << tok::l_paren; |
| 1037 | return true; |
| 1038 | } |
| 1039 | } |
| 1040 | do { |
| 1041 | // Walk over a component of a nested-name-specifier. |
| 1042 | if (Tok.is(K: tok::coloncolon)) { |
| 1043 | Toks.push_back(Elt: Tok); |
| 1044 | ConsumeToken(); |
| 1045 | |
| 1046 | if (Tok.is(K: tok::kw_template)) { |
| 1047 | Toks.push_back(Elt: Tok); |
| 1048 | ConsumeToken(); |
| 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | if (Tok.is(K: tok::identifier)) { |
| 1053 | Toks.push_back(Elt: Tok); |
| 1054 | ConsumeToken(); |
| 1055 | } else { |
| 1056 | break; |
| 1057 | } |
| 1058 | // Pack indexing |
| 1059 | if (Tok.is(K: tok::ellipsis) && NextToken().is(K: tok::l_square)) { |
| 1060 | Toks.push_back(Elt: Tok); |
| 1061 | SourceLocation OpenLoc = ConsumeToken(); |
| 1062 | Toks.push_back(Elt: Tok); |
| 1063 | ConsumeBracket(); |
| 1064 | if (!ConsumeAndStoreUntil(T1: tok::r_square, Toks, /*StopAtSemi=*/true)) { |
| 1065 | Diag(Loc: Tok.getLocation(), DiagID: diag::err_expected) << tok::r_square; |
| 1066 | Diag(Loc: OpenLoc, DiagID: diag::note_matching) << tok::l_square; |
| 1067 | return true; |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | } while (Tok.is(K: tok::coloncolon)); |
| 1072 | |
| 1073 | if (Tok.is(K: tok::code_completion)) { |
| 1074 | Toks.push_back(Elt: Tok); |
| 1075 | ConsumeCodeCompletionToken(); |
| 1076 | if (Tok.isOneOf(Ks: tok::identifier, Ks: tok::coloncolon, Ks: tok::kw_decltype)) { |
| 1077 | // Could be the start of another member initializer (the ',' has not |
| 1078 | // been written yet) |
| 1079 | continue; |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | if (Tok.is(K: tok::comma)) { |
| 1084 | // The initialization is missing, we'll diagnose it later. |
| 1085 | Toks.push_back(Elt: Tok); |
| 1086 | ConsumeToken(); |
| 1087 | continue; |
| 1088 | } |
| 1089 | if (Tok.is(K: tok::less)) |
| 1090 | MightBeTemplateArgument = true; |
| 1091 | |
| 1092 | if (MightBeTemplateArgument) { |
| 1093 | // We may be inside a template argument list. Grab up to the start of the |
| 1094 | // next parenthesized initializer or braced-init-list. This *might* be the |
| 1095 | // initializer, or it might be a subexpression in the template argument |
| 1096 | // list. |
| 1097 | // FIXME: Count angle brackets, and clear MightBeTemplateArgument |
| 1098 | // if all angles are closed. |
| 1099 | if (!ConsumeAndStoreUntil(T1: tok::l_paren, T2: tok::l_brace, Toks, |
| 1100 | /*StopAtSemi=*/true, |
| 1101 | /*ConsumeFinalToken=*/false)) { |
| 1102 | // We're not just missing the initializer, we're also missing the |
| 1103 | // function body! |
| 1104 | return Diag(Loc: Tok.getLocation(), DiagID: diag::err_expected) << tok::l_brace; |
| 1105 | } |
| 1106 | } else if (Tok.isNot(K: tok::l_paren) && Tok.isNot(K: tok::l_brace)) { |
| 1107 | // We found something weird in a mem-initializer-id. |
| 1108 | if (getLangOpts().CPlusPlus11) |
| 1109 | return Diag(Loc: Tok.getLocation(), DiagID: diag::err_expected_either) |
| 1110 | << tok::l_paren << tok::l_brace; |
| 1111 | else |
| 1112 | return Diag(Loc: Tok.getLocation(), DiagID: diag::err_expected) << tok::l_paren; |
| 1113 | } |
| 1114 | |
| 1115 | tok::TokenKind kind = Tok.getKind(); |
| 1116 | Toks.push_back(Elt: Tok); |
| 1117 | bool IsLParen = (kind == tok::l_paren); |
| 1118 | SourceLocation OpenLoc = Tok.getLocation(); |
| 1119 | |
| 1120 | if (IsLParen) { |
| 1121 | ConsumeParen(); |
| 1122 | } else { |
| 1123 | assert(kind == tok::l_brace && "Must be left paren or brace here." ); |
| 1124 | ConsumeBrace(); |
| 1125 | // In C++03, this has to be the start of the function body, which |
| 1126 | // means the initializer is malformed; we'll diagnose it later. |
| 1127 | if (!getLangOpts().CPlusPlus11) |
| 1128 | return false; |
| 1129 | |
| 1130 | const Token &PreviousToken = Toks[Toks.size() - 2]; |
| 1131 | if (!MightBeTemplateArgument && |
| 1132 | !PreviousToken.isOneOf(Ks: tok::identifier, Ks: tok::greater, |
| 1133 | Ks: tok::greatergreater)) { |
| 1134 | // If the opening brace is not preceded by one of these tokens, we are |
| 1135 | // missing the mem-initializer-id. In order to recover better, we need |
| 1136 | // to use heuristics to determine if this '{' is most likely the |
| 1137 | // beginning of a brace-init-list or the function body. |
| 1138 | // Check the token after the corresponding '}'. |
| 1139 | TentativeParsingAction PA(*this); |
| 1140 | if (SkipUntil(T: tok::r_brace) && |
| 1141 | !Tok.isOneOf(Ks: tok::comma, Ks: tok::ellipsis, Ks: tok::l_brace)) { |
| 1142 | // Consider there was a malformed initializer and this is the start |
| 1143 | // of the function body. We'll diagnose it later. |
| 1144 | PA.Revert(); |
| 1145 | return false; |
| 1146 | } |
| 1147 | PA.Revert(); |
| 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | // Grab the initializer (or the subexpression of the template argument). |
| 1152 | // FIXME: If we support lambdas here, we'll need to set StopAtSemi to false |
| 1153 | // if we might be inside the braces of a lambda-expression. |
| 1154 | tok::TokenKind CloseKind = IsLParen ? tok::r_paren : tok::r_brace; |
| 1155 | if (!ConsumeAndStoreUntil(T1: CloseKind, Toks, /*StopAtSemi=*/true)) { |
| 1156 | Diag(Tok, DiagID: diag::err_expected) << CloseKind; |
| 1157 | Diag(Loc: OpenLoc, DiagID: diag::note_matching) << kind; |
| 1158 | return true; |
| 1159 | } |
| 1160 | |
| 1161 | // Grab pack ellipsis, if present. |
| 1162 | if (Tok.is(K: tok::ellipsis)) { |
| 1163 | Toks.push_back(Elt: Tok); |
| 1164 | ConsumeToken(); |
| 1165 | } |
| 1166 | |
| 1167 | // If we know we just consumed a mem-initializer, we must have ',' or '{' |
| 1168 | // next. |
| 1169 | if (Tok.is(K: tok::comma)) { |
| 1170 | Toks.push_back(Elt: Tok); |
| 1171 | ConsumeToken(); |
| 1172 | } else if (Tok.is(K: tok::l_brace)) { |
| 1173 | // This is the function body if the ')' or '}' is immediately followed by |
| 1174 | // a '{'. That cannot happen within a template argument, apart from the |
| 1175 | // case where a template argument contains a compound literal: |
| 1176 | // |
| 1177 | // S ( ) : a < b < c > ( d ) { } |
| 1178 | // // End of declaration, or still inside the template argument? |
| 1179 | // |
| 1180 | // ... and the case where the template argument contains a lambda: |
| 1181 | // |
| 1182 | // S ( ) : a < 0 && b < c > ( d ) + [ ] ( ) { return 0; } |
| 1183 | // ( ) > ( ) { } |
| 1184 | // |
| 1185 | // FIXME: Disambiguate these cases. Note that the latter case is probably |
| 1186 | // going to be made ill-formed by core issue 1607. |
| 1187 | Toks.push_back(Elt: Tok); |
| 1188 | ConsumeBrace(); |
| 1189 | return false; |
| 1190 | } else if (!MightBeTemplateArgument) { |
| 1191 | return Diag(Loc: Tok.getLocation(), DiagID: diag::err_expected_either) << tok::l_brace |
| 1192 | << tok::comma; |
| 1193 | } |
| 1194 | } |
| 1195 | } |
| 1196 | |
| 1197 | bool Parser::ConsumeAndStoreConditional(CachedTokens &Toks) { |
| 1198 | // Consume '?'. |
| 1199 | assert(Tok.is(tok::question)); |
| 1200 | Toks.push_back(Elt: Tok); |
| 1201 | ConsumeToken(); |
| 1202 | |
| 1203 | while (Tok.isNot(K: tok::colon)) { |
| 1204 | if (!ConsumeAndStoreUntil(T1: tok::question, T2: tok::colon, Toks, |
| 1205 | /*StopAtSemi=*/true, |
| 1206 | /*ConsumeFinalToken=*/false)) |
| 1207 | return false; |
| 1208 | |
| 1209 | // If we found a nested conditional, consume it. |
| 1210 | if (Tok.is(K: tok::question) && !ConsumeAndStoreConditional(Toks)) |
| 1211 | return false; |
| 1212 | } |
| 1213 | |
| 1214 | // Consume ':'. |
| 1215 | Toks.push_back(Elt: Tok); |
| 1216 | ConsumeToken(); |
| 1217 | return true; |
| 1218 | } |
| 1219 | |
| 1220 | bool Parser::ConsumeAndStoreInitializer(CachedTokens &Toks, |
| 1221 | CachedInitKind CIK) { |
| 1222 | // We always want this function to consume at least one token if not at EOF. |
| 1223 | bool IsFirstToken = true; |
| 1224 | |
| 1225 | // Number of possible unclosed <s we've seen so far. These might be templates, |
| 1226 | // and might not, but if there were none of them (or we know for sure that |
| 1227 | // we're within a template), we can avoid a tentative parse. |
| 1228 | unsigned AngleCount = 0; |
| 1229 | unsigned KnownTemplateCount = 0; |
| 1230 | |
| 1231 | while (true) { |
| 1232 | switch (Tok.getKind()) { |
| 1233 | case tok::ellipsis: |
| 1234 | // We found an elipsis at the end of the parameter list; |
| 1235 | // it is not part of a parameter declaration. |
| 1236 | if (ParenCount == 1 && NextToken().is(K: tok::r_paren)) |
| 1237 | return true; |
| 1238 | goto consume_token; |
| 1239 | case tok::comma: |
| 1240 | // If we might be in a template, perform a tentative parse to check. |
| 1241 | if (!AngleCount) |
| 1242 | // Not a template argument: this is the end of the initializer. |
| 1243 | return true; |
| 1244 | if (KnownTemplateCount) |
| 1245 | goto consume_token; |
| 1246 | |
| 1247 | // We hit a comma inside angle brackets. This is the hard case. The |
| 1248 | // rule we follow is: |
| 1249 | // * For a default argument, if the tokens after the comma form a |
| 1250 | // syntactically-valid parameter-declaration-clause, in which each |
| 1251 | // parameter has an initializer, then this comma ends the default |
| 1252 | // argument. |
| 1253 | // * For a default initializer, if the tokens after the comma form a |
| 1254 | // syntactically-valid init-declarator-list, then this comma ends |
| 1255 | // the default initializer. |
| 1256 | { |
| 1257 | TentativeParsingAction TPA(*this, /*Unannotated=*/true); |
| 1258 | Sema::TentativeAnalysisScope Scope(Actions); |
| 1259 | |
| 1260 | TPResult Result = TPResult::Error; |
| 1261 | ConsumeToken(); |
| 1262 | switch (CIK) { |
| 1263 | case CachedInitKind::DefaultInitializer: |
| 1264 | Result = TryParseInitDeclaratorList(); |
| 1265 | // If we parsed a complete, ambiguous init-declarator-list, this |
| 1266 | // is only syntactically-valid if it's followed by a semicolon. |
| 1267 | if (Result == TPResult::Ambiguous && Tok.isNot(K: tok::semi)) |
| 1268 | Result = TPResult::False; |
| 1269 | break; |
| 1270 | |
| 1271 | case CachedInitKind::DefaultArgument: |
| 1272 | bool InvalidAsDeclaration = false; |
| 1273 | Result = TryParseParameterDeclarationClause( |
| 1274 | InvalidAsDeclaration: &InvalidAsDeclaration, /*VersusTemplateArg=*/true); |
| 1275 | // If this is an expression or a declaration with a missing |
| 1276 | // 'typename', assume it's not a declaration. |
| 1277 | if (Result == TPResult::Ambiguous && InvalidAsDeclaration) |
| 1278 | Result = TPResult::False; |
| 1279 | break; |
| 1280 | } |
| 1281 | |
| 1282 | // Put the token stream back and undo any annotations we performed |
| 1283 | // after the comma. They may reflect a different parse than the one |
| 1284 | // we will actually perform at the end of the class. |
| 1285 | TPA.Revert(); |
| 1286 | |
| 1287 | // If what follows could be a declaration, it is a declaration. |
| 1288 | if (Result != TPResult::False && Result != TPResult::Error) |
| 1289 | return true; |
| 1290 | } |
| 1291 | |
| 1292 | // Keep going. We know we're inside a template argument list now. |
| 1293 | ++KnownTemplateCount; |
| 1294 | goto consume_token; |
| 1295 | |
| 1296 | case tok::eof: |
| 1297 | // Ran out of tokens. |
| 1298 | return false; |
| 1299 | |
| 1300 | case tok::less: |
| 1301 | // FIXME: A '<' can only start a template-id if it's preceded by an |
| 1302 | // identifier, an operator-function-id, or a literal-operator-id. |
| 1303 | ++AngleCount; |
| 1304 | goto consume_token; |
| 1305 | |
| 1306 | case tok::question: |
| 1307 | // In 'a ? b : c', 'b' can contain an unparenthesized comma. If it does, |
| 1308 | // that is *never* the end of the initializer. Skip to the ':'. |
| 1309 | if (!ConsumeAndStoreConditional(Toks)) |
| 1310 | return false; |
| 1311 | break; |
| 1312 | |
| 1313 | case tok::greatergreatergreater: |
| 1314 | if (!getLangOpts().CPlusPlus11) |
| 1315 | goto consume_token; |
| 1316 | if (AngleCount) --AngleCount; |
| 1317 | if (KnownTemplateCount) --KnownTemplateCount; |
| 1318 | [[fallthrough]]; |
| 1319 | case tok::greatergreater: |
| 1320 | if (!getLangOpts().CPlusPlus11) |
| 1321 | goto consume_token; |
| 1322 | if (AngleCount) --AngleCount; |
| 1323 | if (KnownTemplateCount) --KnownTemplateCount; |
| 1324 | [[fallthrough]]; |
| 1325 | case tok::greater: |
| 1326 | if (AngleCount) --AngleCount; |
| 1327 | if (KnownTemplateCount) --KnownTemplateCount; |
| 1328 | goto consume_token; |
| 1329 | |
| 1330 | case tok::kw_template: |
| 1331 | // 'template' identifier '<' is known to start a template argument list, |
| 1332 | // and can be used to disambiguate the parse. |
| 1333 | // FIXME: Support all forms of 'template' unqualified-id '<'. |
| 1334 | Toks.push_back(Elt: Tok); |
| 1335 | ConsumeToken(); |
| 1336 | if (Tok.is(K: tok::identifier)) { |
| 1337 | Toks.push_back(Elt: Tok); |
| 1338 | ConsumeToken(); |
| 1339 | if (Tok.is(K: tok::less)) { |
| 1340 | ++AngleCount; |
| 1341 | ++KnownTemplateCount; |
| 1342 | Toks.push_back(Elt: Tok); |
| 1343 | ConsumeToken(); |
| 1344 | } |
| 1345 | } |
| 1346 | break; |
| 1347 | |
| 1348 | case tok::kw_operator: |
| 1349 | // If 'operator' precedes other punctuation, that punctuation loses |
| 1350 | // its special behavior. |
| 1351 | Toks.push_back(Elt: Tok); |
| 1352 | ConsumeToken(); |
| 1353 | switch (Tok.getKind()) { |
| 1354 | case tok::comma: |
| 1355 | case tok::greatergreatergreater: |
| 1356 | case tok::greatergreater: |
| 1357 | case tok::greater: |
| 1358 | case tok::less: |
| 1359 | Toks.push_back(Elt: Tok); |
| 1360 | ConsumeToken(); |
| 1361 | break; |
| 1362 | default: |
| 1363 | break; |
| 1364 | } |
| 1365 | break; |
| 1366 | |
| 1367 | case tok::l_paren: |
| 1368 | // Recursively consume properly-nested parens. |
| 1369 | Toks.push_back(Elt: Tok); |
| 1370 | ConsumeParen(); |
| 1371 | ConsumeAndStoreUntil(T1: tok::r_paren, Toks, /*StopAtSemi=*/false); |
| 1372 | break; |
| 1373 | case tok::l_square: |
| 1374 | // Recursively consume properly-nested square brackets. |
| 1375 | Toks.push_back(Elt: Tok); |
| 1376 | ConsumeBracket(); |
| 1377 | ConsumeAndStoreUntil(T1: tok::r_square, Toks, /*StopAtSemi=*/false); |
| 1378 | break; |
| 1379 | case tok::l_brace: |
| 1380 | // Recursively consume properly-nested braces. |
| 1381 | Toks.push_back(Elt: Tok); |
| 1382 | ConsumeBrace(); |
| 1383 | ConsumeAndStoreUntil(T1: tok::r_brace, Toks, /*StopAtSemi=*/false); |
| 1384 | break; |
| 1385 | |
| 1386 | // Okay, we found a ']' or '}' or ')', which we think should be balanced. |
| 1387 | // Since the user wasn't looking for this token (if they were, it would |
| 1388 | // already be handled), this isn't balanced. If there is a LHS token at a |
| 1389 | // higher level, we will assume that this matches the unbalanced token |
| 1390 | // and return it. Otherwise, this is a spurious RHS token, which we |
| 1391 | // consume and pass on to downstream code to diagnose. |
| 1392 | case tok::r_paren: |
| 1393 | if (CIK == CachedInitKind::DefaultArgument) |
| 1394 | return true; // End of the default argument. |
| 1395 | if (ParenCount && !IsFirstToken) |
| 1396 | return false; |
| 1397 | Toks.push_back(Elt: Tok); |
| 1398 | ConsumeParen(); |
| 1399 | continue; |
| 1400 | case tok::r_square: |
| 1401 | if (BracketCount && !IsFirstToken) |
| 1402 | return false; |
| 1403 | Toks.push_back(Elt: Tok); |
| 1404 | ConsumeBracket(); |
| 1405 | continue; |
| 1406 | case tok::r_brace: |
| 1407 | if (BraceCount && !IsFirstToken) |
| 1408 | return false; |
| 1409 | Toks.push_back(Elt: Tok); |
| 1410 | ConsumeBrace(); |
| 1411 | continue; |
| 1412 | |
| 1413 | case tok::code_completion: |
| 1414 | Toks.push_back(Elt: Tok); |
| 1415 | ConsumeCodeCompletionToken(); |
| 1416 | break; |
| 1417 | |
| 1418 | case tok::string_literal: |
| 1419 | case tok::wide_string_literal: |
| 1420 | case tok::utf8_string_literal: |
| 1421 | case tok::utf16_string_literal: |
| 1422 | case tok::utf32_string_literal: |
| 1423 | Toks.push_back(Elt: Tok); |
| 1424 | ConsumeStringToken(); |
| 1425 | break; |
| 1426 | case tok::semi: |
| 1427 | if (CIK == CachedInitKind::DefaultInitializer) |
| 1428 | return true; // End of the default initializer. |
| 1429 | [[fallthrough]]; |
| 1430 | default: |
| 1431 | consume_token: |
| 1432 | // If it's an annotation token, then we've run out of tokens and should |
| 1433 | // bail out. Otherwise, cache the token and consume it. |
| 1434 | if (Tok.isAnnotation()) |
| 1435 | return false; |
| 1436 | |
| 1437 | Toks.push_back(Elt: Tok); |
| 1438 | ConsumeToken(); |
| 1439 | break; |
| 1440 | } |
| 1441 | IsFirstToken = false; |
| 1442 | } |
| 1443 | } |
| 1444 | |