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