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