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