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