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