Coverage Report

Created: 2021-01-23 06:44

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Parse/ParseCXXInlineMethods.cpp
Line
Count
Source (jump to first uncovered line)
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
641k
    SourceLocation PureSpecLoc) {
28
641k
  assert(D.isFunctionDeclarator() && "This isn't a function declarator!");
29
641k
  assert(Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try, tok::equal) &&
30
641k
         "Current token not a '{', ':', '=', or 'try'!");
31
32
641k
  MultiTemplateParamsArg TemplateParams(
33
94.3k
      TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data()
34
547k
                                  : nullptr,
35
547k
      TemplateInfo.TemplateParams ? 
TemplateInfo.TemplateParams->size()94.3k
: 0);
36
37
641k
  NamedDecl *FnD;
38
641k
  if (D.getDeclSpec().isFriendSpecified())
39
10.7k
    FnD = Actions.ActOnFriendFunctionDecl(getCurScope(), D,
40
10.7k
                                          TemplateParams);
41
631k
  else {
42
631k
    FnD = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS, D,
43
631k
                                           TemplateParams, nullptr,
44
631k
                                           VS, ICIS_NoInit);
45
631k
    if (FnD) {
46
630k
      Actions.ProcessDeclAttributeList(getCurScope(), FnD, AccessAttrs);
47
630k
      if (PureSpecLoc.isValid())
48
3
        Actions.ActOnPureSpecifier(FnD, PureSpecLoc);
49
630k
    }
50
631k
  }
51
52
641k
  if (FnD)
53
641k
    HandleMemberFunctionDeclDelays(D, FnD);
54
55
641k
  D.complete(FnD);
56
57
641k
  if (TryConsumeToken(tok::equal)) {
58
31.2k
    if (!FnD) {
59
1
      SkipUntil(tok::semi);
60
1
      return nullptr;
61
1
    }
62
63
31.2k
    bool Delete = false;
64
31.2k
    SourceLocation KWLoc;
65
31.2k
    SourceLocation KWEndLoc = Tok.getEndLoc().getLocWithOffset(-1);
66
31.2k
    if (TryConsumeToken(tok::kw_delete, KWLoc)) {
67
10.0k
      Diag(KWLoc, getLangOpts().CPlusPlus11
68
10.0k
                      ? diag::warn_cxx98_compat_defaulted_deleted_function
69
20
                      : diag::ext_defaulted_deleted_function)
70
10.0k
        << 1 /* deleted */;
71
10.0k
      Actions.SetDeclDeleted(FnD, KWLoc);
72
10.0k
      Delete = true;
73
10.0k
      if (auto *DeclAsFunction = dyn_cast<FunctionDecl>(FnD)) {
74
8.46k
        DeclAsFunction->setRangeEnd(KWEndLoc);
75
8.46k
      }
76
21.1k
    } else if (TryConsumeToken(tok::kw_default, KWLoc)) {
77
21.1k
      Diag(KWLoc, getLangOpts().CPlusPlus11
78
21.1k
                      ? diag::warn_cxx98_compat_defaulted_deleted_function
79
9
                      : diag::ext_defaulted_deleted_function)
80
21.1k
        << 0 /* defaulted */;
81
21.1k
      Actions.SetDeclDefaulted(FnD, KWLoc);
82
21.1k
      if (auto *DeclAsFunction = dyn_cast<FunctionDecl>(FnD)) {
83
21.1k
        DeclAsFunction->setRangeEnd(KWEndLoc);
84
21.1k
      }
85
0
    } else {
86
0
      llvm_unreachable("function definition after = not 'delete' or 'default'");
87
0
    }
88
89
31.2k
    if (Tok.is(tok::comma)) {
90
0
      Diag(KWLoc, diag::err_default_delete_in_multiple_declaration)
91
0
        << Delete;
92
0
      SkipUntil(tok::semi);
93
31.2k
    } else if (ExpectAndConsume(tok::semi, diag::err_expected_after,
94
21.1k
                                Delete ? 
"delete"10.0k
: "default")) {
95
1
      SkipUntil(tok::semi);
96
1
    }
97
98
31.2k
    return FnD;
99
31.2k
  }
100
101
610k
  if (SkipFunctionBodies && 
(371
!FnD371
||
Actions.canSkipFunctionBody(FnD)371
) &&
102
364
      trySkippingFunctionBody()) {
103
307
    Actions.ActOnSkippedFunctionBody(FnD);
104
307
    return FnD;
105
307
  }
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
610k
  if (getLangOpts().DelayedTemplateParsing &&
111
763
      D.getFunctionDefinitionKind() == FunctionDefinitionKind::Definition &&
112
763
      !D.getDeclSpec().hasConstexprSpecifier() &&
113
747
      !(FnD && FnD->getAsFunction() &&
114
747
        FnD->getAsFunction()->getReturnType()->getContainedAutoType()) &&
115
694
      ((Actions.CurContext->isDependentContext() ||
116
468
        (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
117
37
         TemplateInfo.Kind != ParsedTemplateInfo::ExplicitSpecialization)) &&
118
259
       !Actions.IsInsideALocalClassWithinATemplateFunction())) {
119
120
239
    CachedTokens Toks;
121
239
    LexTemplateFunctionForLateParsing(Toks);
122
123
239
    if (FnD) {
124
239
      FunctionDecl *FD = FnD->getAsFunction();
125
239
      Actions.CheckForFunctionRedefinition(FD);
126
239
      Actions.MarkAsLateParsedTemplate(FD, FnD, Toks);
127
239
    }
128
129
239
    return FnD;
130
239
  }
131
132
  // Consume the tokens and store them for later parsing.
133
134
610k
  LexedMethod* LM = new LexedMethod(this, FnD);
135
610k
  getCurrentClass().LateParsedDeclarations.push_back(LM);
136
610k
  CachedTokens &Toks = LM->Toks;
137
138
610k
  tok::TokenKind kind = Tok.getKind();
139
  // Consume everything up to (and including) the left brace of the
140
  // function body.
141
610k
  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
16
    SkipMalformedDecl();
148
16
    delete getCurrentClass().LateParsedDeclarations.back();
149
16
    getCurrentClass().LateParsedDeclarations.pop_back();
150
16
    return FnD;
151
610k
  } else {
152
    // Consume everything up to (and including) the matching right brace.
153
610k
    ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
154
610k
  }
155
156
  // If we're in a function-try-block, we need to store all the catch blocks.
157
610k
  if (kind == tok::kw_try) {
158
74
    while (Tok.is(tok::kw_catch)) {
159
37
      ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
160
37
      ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
161
37
    }
162
37
  }
163
164
610k
  if (FnD) {
165
609k
    FunctionDecl *FD = FnD->getAsFunction();
166
    // Track that this function will eventually have a body; Sema needs
167
    // to know this.
168
609k
    Actions.CheckForFunctionRedefinition(FD);
169
609k
    FD->setWillHaveBody(true);
170
28
  } else {
171
    // If semantic analysis could not build a function declaration,
172
    // just throw away the late-parsed declaration.
173
28
    delete getCurrentClass().LateParsedDeclarations.back();
174
28
    getCurrentClass().LateParsedDeclarations.pop_back();
175
28
  }
176
177
610k
  return FnD;
178
610k
}
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
2.96k
void Parser::ParseCXXNonStaticMemberInitializer(Decl *VarD) {
185
2.96k
  assert(Tok.isOneOf(tok::l_brace, tok::equal) &&
186
2.96k
         "Current token not a '{' or '='!");
187
188
2.96k
  LateParsedMemberInitializer *MI =
189
2.96k
    new LateParsedMemberInitializer(this, VarD);
190
2.96k
  getCurrentClass().LateParsedDeclarations.push_back(MI);
191
2.96k
  CachedTokens &Toks = MI->Toks;
192
193
2.96k
  tok::TokenKind kind = Tok.getKind();
194
2.96k
  if (kind == tok::equal) {
195
2.89k
    Toks.push_back(Tok);
196
2.89k
    ConsumeToken();
197
2.89k
  }
198
199
2.96k
  if (kind == tok::l_brace) {
200
    // Begin by storing the '{' token.
201
68
    Toks.push_back(Tok);
202
68
    ConsumeBrace();
203
204
    // Consume everything up to (and including) the matching right brace.
205
68
    ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/true);
206
2.89k
  } else {
207
    // Consume everything up to (but excluding) the comma or semicolon.
208
2.89k
    ConsumeAndStoreInitializer(Toks, CIK_DefaultInitializer);
209
2.89k
  }
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
2.96k
  Token Eof;
214
2.96k
  Eof.startToken();
215
2.96k
  Eof.setKind(tok::eof);
216
2.96k
  Eof.setLocation(Tok.getLocation());
217
2.96k
  Eof.setEofData(VarD);
218
2.96k
  Toks.push_back(Eof);
219
2.96k
}
220
221
727k
Parser::LateParsedDeclaration::~LateParsedDeclaration() {}
222
618k
void Parser::LateParsedDeclaration::ParseLexedMethodDeclarations() {}
223
705k
void Parser::LateParsedDeclaration::ParseLexedMemberInitializers() {}
224
98.2k
void Parser::LateParsedDeclaration::ParseLexedMethodDefs() {}
225
702k
void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
226
708k
void Parser::LateParsedDeclaration::ParseLexedPragmas() {}
227
228
Parser::LateParsedClass::LateParsedClass(Parser *P, ParsingClass *C)
229
7.10k
  : Self(P), Class(C) {}
230
231
7.10k
Parser::LateParsedClass::~LateParsedClass() {
232
7.10k
  Self->DeallocateParsedClasses(Class);
233
7.10k
}
234
235
7.10k
void Parser::LateParsedClass::ParseLexedMethodDeclarations() {
236
7.10k
  Self->ParseLexedMethodDeclarations(*Class);
237
7.10k
}
238
239
7.10k
void Parser::LateParsedClass::ParseLexedMemberInitializers() {
240
7.10k
  Self->ParseLexedMemberInitializers(*Class);
241
7.10k
}
242
243
7.10k
void Parser::LateParsedClass::ParseLexedMethodDefs() {
244
7.10k
  Self->ParseLexedMethodDefs(*Class);
245
7.10k
}
246
247
7.10k
void Parser::LateParsedClass::ParseLexedAttributes() {
248
7.10k
  Self->ParseLexedAttributes(*Class);
249
7.10k
}
250
251
7.10k
void Parser::LateParsedClass::ParseLexedPragmas() {
252
7.10k
  Self->ParseLexedPragmas(*Class);
253
7.10k
}
254
255
89.9k
void Parser::LateParsedMethodDeclaration::ParseLexedMethodDeclarations() {
256
89.9k
  Self->ParseLexedMethodDeclaration(*this);
257
89.9k
}
258
259
609k
void Parser::LexedMethod::ParseLexedMethodDefs() {
260
609k
  Self->ParseLexedMethodDef(*this);
261
609k
}
262
263
2.96k
void Parser::LateParsedMemberInitializer::ParseLexedMemberInitializers() {
264
2.96k
  Self->ParseLexedMemberInitializer(*this);
265
2.96k
}
266
267
5.22k
void Parser::LateParsedAttribute::ParseLexedAttributes() {
268
5.22k
  Self->ParseLexedAttribute(*this, true, false);
269
5.22k
}
270
271
86
void Parser::LateParsedPragma::ParseLexedPragmas() {
272
86
  Self->ParseLexedPragma(*this);
273
86
}
274
275
/// Utility to re-enter a possibly-templated scope while parsing its
276
/// late-parsed components.
277
struct Parser::ReenterTemplateScopeRAII {
278
  Parser &P;
279
  MultiParseScope Scopes;
280
  TemplateParameterDepthRAII CurTemplateDepthTracker;
281
282
  ReenterTemplateScopeRAII(Parser &P, Decl *MaybeTemplated, bool Enter = true)
283
3.76M
      : P(P), Scopes(P), CurTemplateDepthTracker(P.TemplateParameterDepth) {
284
3.76M
    if (Enter) {
285
741k
      CurTemplateDepthTracker.addDepth(
286
741k
          P.ReenterTemplateScopes(Scopes, MaybeTemplated));
287
741k
    }
288
3.76M
  }
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
3.05M
        Class(Class) {
299
    // If this is the top-level class, we're still within its scope.
300
3.05M
    if (Class.TopLevelClass)
301
3.02M
      return;
302
303
    // Re-enter the class scope itself.
304
35.5k
    Scopes.Enter(Scope::ClassScope|Scope::DeclScope);
305
35.5k
    P.Actions.ActOnStartDelayedMemberDeclarations(P.getCurScope(),
306
35.5k
                                                  Class.TagOrTemplate);
307
35.5k
  }
308
3.05M
  ~ReenterClassScopeRAII() {
309
3.05M
    if (Class.TopLevelClass)
310
3.02M
      return;
311
312
35.5k
    P.Actions.ActOnFinishDelayedMemberDeclarations(P.getCurScope(),
313
35.5k
                                                   Class.TagOrTemplate);
314
35.5k
  }
315
};
316
317
/// ParseLexedMethodDeclarations - We finished parsing the member
318
/// specification of a top (non-nested) C++ class. Now go over the
319
/// stack of method declarations with some parts for which parsing was
320
/// delayed (such as default arguments) and parse them.
321
611k
void Parser::ParseLexedMethodDeclarations(ParsingClass &Class) {
322
611k
  ReenterClassScopeRAII InClassScope(*this, Class);
323
324
611k
  for (LateParsedDeclaration *LateD : Class.LateParsedDeclarations)
325
715k
    LateD->ParseLexedMethodDeclarations();
326
611k
}
327
328
89.9k
void Parser::ParseLexedMethodDeclaration(LateParsedMethodDeclaration &LM) {
329
  // If this is a member template, introduce the template parameter scope.
330
89.9k
  ReenterTemplateScopeRAII InFunctionTemplateScope(*this, LM.Method);
331
332
  // Start the delayed C++ method declaration
333
89.9k
  Actions.ActOnStartDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
334
335
  // Introduce the parameters into scope and parse their default
336
  // arguments.
337
89.9k
  InFunctionTemplateScope.Scopes.Enter(Scope::FunctionPrototypeScope |
338
89.9k
                                       Scope::FunctionDeclarationScope |
339
89.9k
                                       Scope::DeclScope);
340
249k
  for (unsigned I = 0, N = LM.DefaultArgs.size(); I != N; 
++I159k
) {
341
159k
    auto Param = cast<ParmVarDecl>(LM.DefaultArgs[I].Param);
342
    // Introduce the parameter into scope.
343
159k
    bool HasUnparsed = Param->hasUnparsedDefaultArg();
344
159k
    Actions.ActOnDelayedCXXMethodParameter(getCurScope(), Param);
345
159k
    std::unique_ptr<CachedTokens> Toks = std::move(LM.DefaultArgs[I].Toks);
346
159k
    if (Toks) {
347
65.3k
      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
65.3k
      Token LastDefaultArgToken = Toks->back();
352
65.3k
      Token DefArgEnd;
353
65.3k
      DefArgEnd.startToken();
354
65.3k
      DefArgEnd.setKind(tok::eof);
355
65.3k
      DefArgEnd.setLocation(LastDefaultArgToken.getEndLoc());
356
65.3k
      DefArgEnd.setEofData(Param);
357
65.3k
      Toks->push_back(DefArgEnd);
358
359
      // Parse the default argument from its saved token stream.
360
65.3k
      Toks->push_back(Tok); // So that the current token doesn't get lost
361
65.3k
      PP.EnterTokenStream(*Toks, true, /*IsReinject*/ true);
362
363
      // Consume the previously-pushed token.
364
65.3k
      ConsumeAnyToken();
365
366
      // Consume the '='.
367
65.3k
      assert(Tok.is(tok::equal) && "Default argument not starting with '='");
368
65.3k
      SourceLocation EqualLoc = ConsumeToken();
369
370
      // The argument isn't actually potentially evaluated unless it is
371
      // used.
372
65.3k
      EnterExpressionEvaluationContext Eval(
373
65.3k
          Actions,
374
65.3k
          Sema::ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed, Param);
375
376
65.3k
      ExprResult DefArgResult;
377
65.3k
      if (getLangOpts().CPlusPlus11 && 
Tok.is(tok::l_brace)64.9k
) {
378
8
        Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
379
8
        DefArgResult = ParseBraceInitializer();
380
8
      } else
381
65.3k
        DefArgResult = ParseAssignmentExpression();
382
65.3k
      DefArgResult = Actions.CorrectDelayedTyposInExpr(DefArgResult);
383
65.3k
      if (DefArgResult.isInvalid()) {
384
25
        Actions.ActOnParamDefaultArgumentError(Param, EqualLoc);
385
65.3k
      } else {
386
65.3k
        if (Tok.isNot(tok::eof) || 
Tok.getEofData() != Param65.3k
) {
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
2
          assert(Toks->size() >= 3 && "expected a token in default arg");
391
2
          Diag(Tok.getLocation(), diag::err_default_arg_unparsed)
392
2
            << SourceRange(Tok.getLocation(),
393
2
                           (*Toks)[Toks->size() - 3].getLocation());
394
2
        }
395
65.3k
        Actions.ActOnParamDefaultArgument(Param, EqualLoc,
396
65.3k
                                          DefArgResult.get());
397
65.3k
      }
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
65.3k
      while (Tok.isNot(tok::eof))
402
7
        ConsumeAnyToken();
403
404
65.3k
      if (Tok.is(tok::eof) && Tok.getEofData() == Param)
405
65.3k
        ConsumeAnyToken();
406
94.0k
    } else if (HasUnparsed) {
407
5
      assert(Param->hasInheritedDefaultArg());
408
5
      const FunctionDecl *Old;
409
5
      if (const auto *FunTmpl = dyn_cast<FunctionTemplateDecl>(LM.Method))
410
1
        Old =
411
1
            cast<FunctionDecl>(FunTmpl->getTemplatedDecl())->getPreviousDecl();
412
4
      else
413
4
        Old = cast<FunctionDecl>(LM.Method)->getPreviousDecl();
414
5
      if (Old) {
415
3
        ParmVarDecl *OldParam = const_cast<ParmVarDecl*>(Old->getParamDecl(I));
416
3
        assert(!OldParam->hasUnparsedDefaultArg());
417
3
        if (OldParam->hasUninstantiatedDefaultArg())
418
2
          Param->setUninstantiatedDefaultArg(
419
2
              OldParam->getUninstantiatedDefaultArg());
420
1
        else
421
1
          Param->setDefaultArg(OldParam->getInit());
422
3
      }
423
5
    }
424
159k
  }
425
426
  // Parse a delayed exception-specification, if there is one.
427
89.9k
  if (CachedTokens *Toks = LM.ExceptionSpecTokens) {
428
29.6k
    ParenBraceBracketBalancer BalancerRAIIObj(*this);
429
430
    // Add the 'stop' token.
431
29.6k
    Token LastExceptionSpecToken = Toks->back();
432
29.6k
    Token ExceptionSpecEnd;
433
29.6k
    ExceptionSpecEnd.startToken();
434
29.6k
    ExceptionSpecEnd.setKind(tok::eof);
435
29.6k
    ExceptionSpecEnd.setLocation(LastExceptionSpecToken.getEndLoc());
436
29.6k
    ExceptionSpecEnd.setEofData(LM.Method);
437
29.6k
    Toks->push_back(ExceptionSpecEnd);
438
439
    // Parse the default argument from its saved token stream.
440
29.6k
    Toks->push_back(Tok); // So that the current token doesn't get lost
441
29.6k
    PP.EnterTokenStream(*Toks, true, /*IsReinject*/true);
442
443
    // Consume the previously-pushed token.
444
29.6k
    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
29.6k
    CXXMethodDecl *Method;
453
29.6k
    if (FunctionTemplateDecl *FunTmpl
454
12.1k
          = dyn_cast<FunctionTemplateDecl>(LM.Method))
455
12.1k
      Method = cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl());
456
17.4k
    else
457
17.4k
      Method = cast<CXXMethodDecl>(LM.Method);
458
459
29.6k
    Sema::CXXThisScopeRAII ThisScope(Actions, Method->getParent(),
460
29.6k
                                     Method->getMethodQualifiers(),
461
29.6k
                                     getLangOpts().CPlusPlus11);
462
463
    // Parse the exception-specification.
464
29.6k
    SourceRange SpecificationRange;
465
29.6k
    SmallVector<ParsedType, 4> DynamicExceptions;
466
29.6k
    SmallVector<SourceRange, 4> DynamicExceptionRanges;
467
29.6k
    ExprResult NoexceptExpr;
468
29.6k
    CachedTokens *ExceptionSpecTokens;
469
470
29.6k
    ExceptionSpecificationType EST
471
29.6k
      = tryParseExceptionSpecification(/*Delayed=*/false, SpecificationRange,
472
29.6k
                                       DynamicExceptions,
473
29.6k
                                       DynamicExceptionRanges, NoexceptExpr,
474
29.6k
                                       ExceptionSpecTokens);
475
476
29.6k
    if (Tok.isNot(tok::eof) || Tok.getEofData() != LM.Method)
477
0
      Diag(Tok.getLocation(), diag::err_except_spec_unparsed);
478
479
    // Attach the exception-specification to the method.
480
29.6k
    Actions.actOnDelayedExceptionSpecification(LM.Method, EST,
481
29.6k
                                               SpecificationRange,
482
29.6k
                                               DynamicExceptions,
483
29.6k
                                               DynamicExceptionRanges,
484
29.6k
                                               NoexceptExpr.isUsable()?
485
28.3k
                                                 NoexceptExpr.get() : 
nullptr1.32k
);
486
487
    // There could be leftover tokens (e.g. because of an error).
488
    // Skip through until we reach the original token position.
489
29.6k
    while (Tok.isNot(tok::eof))
490
0
      ConsumeAnyToken();
491
492
    // Clean up the remaining EOF token.
493
29.6k
    if (Tok.is(tok::eof) && Tok.getEofData() == LM.Method)
494
29.6k
      ConsumeAnyToken();
495
496
29.6k
    delete Toks;
497
29.6k
    LM.ExceptionSpecTokens = nullptr;
498
29.6k
  }
499
500
89.9k
  InFunctionTemplateScope.Scopes.Exit();
501
502
  // Finish the delayed C++ method declaration.
503
89.9k
  Actions.ActOnFinishDelayedCXXMethodDeclaration(getCurScope(), LM.Method);
504
89.9k
}
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
611k
void Parser::ParseLexedMethodDefs(ParsingClass &Class) {
510
611k
  ReenterClassScopeRAII InClassScope(*this, Class);
511
512
611k
  for (LateParsedDeclaration *D : Class.LateParsedDeclarations)
513
715k
    D->ParseLexedMethodDefs();
514
611k
}
515
516
609k
void Parser::ParseLexedMethodDef(LexedMethod &LM) {
517
  // If this is a member template, introduce the template parameter scope.
518
609k
  ReenterTemplateScopeRAII InFunctionTemplateScope(*this, LM.D);
519
520
609k
  ParenBraceBracketBalancer BalancerRAIIObj(*this);
521
522
609k
  assert(!LM.Toks.empty() && "Empty body!");
523
609k
  Token LastBodyToken = LM.Toks.back();
524
609k
  Token BodyEnd;
525
609k
  BodyEnd.startToken();
526
609k
  BodyEnd.setKind(tok::eof);
527
609k
  BodyEnd.setLocation(LastBodyToken.getEndLoc());
528
609k
  BodyEnd.setEofData(LM.D);
529
609k
  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
609k
  LM.Toks.push_back(Tok);
533
609k
  PP.EnterTokenStream(LM.Toks, true, /*IsReinject*/true);
534
535
  // Consume the previously pushed token.
536
609k
  ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
537
609k
  assert(Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try)
538
609k
         && "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
609k
  ParseScope FnScope(this, Scope::FnScope | Scope::DeclScope |
543
609k
                               Scope::CompoundStmtScope);
544
609k
  Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
545
546
609k
  if (Tok.is(tok::kw_try)) {
547
37
    ParseFunctionTryBlock(LM.D, FnScope);
548
549
37
    while (Tok.isNot(tok::eof))
550
0
      ConsumeAnyToken();
551
552
37
    if (Tok.is(tok::eof) && Tok.getEofData() == LM.D)
553
37
      ConsumeAnyToken();
554
37
    return;
555
37
  }
556
609k
  if (Tok.is(tok::colon)) {
557
116k
    ParseConstructorInitializer(LM.D);
558
559
    // Error recovery.
560
116k
    if (!Tok.is(tok::l_brace)) {
561
27
      FnScope.Exit();
562
27
      Actions.ActOnFinishFunctionBody(LM.D, nullptr);
563
564
27
      while (Tok.isNot(tok::eof))
565
0
        ConsumeAnyToken();
566
567
27
      if (Tok.is(tok::eof) && Tok.getEofData() == LM.D)
568
2
        ConsumeAnyToken();
569
27
      return;
570
27
    }
571
493k
  } else
572
493k
    Actions.ActOnDefaultCtorInitializers(LM.D);
573
574
609k
  assert((Actions.getDiagnostics().hasErrorOccurred() ||
575
609k
          !isa<FunctionTemplateDecl>(LM.D) ||
576
609k
          cast<FunctionTemplateDecl>(LM.D)->getTemplateParameters()->getDepth()
577
609k
            < TemplateParameterDepth) &&
578
609k
         "TemplateParameterDepth should be greater than the depth of "
579
609k
         "current template being instantiated!");
580
581
609k
  ParseFunctionStatementBody(LM.D, FnScope);
582
583
609k
  while (Tok.isNot(tok::eof))
584
0
    ConsumeAnyToken();
585
586
609k
  if (Tok.is(tok::eof) && Tok.getEofData() == LM.D)
587
609k
    ConsumeAnyToken();
588
589
609k
  if (auto *FD = dyn_cast_or_null<FunctionDecl>(LM.D))
590
517k
    if (isa<CXXMethodDecl>(FD) ||
591
10.2k
        FD->isInIdentifierNamespace(Decl::IDNS_OrdinaryFriend))
592
517k
      Actions.ActOnFinishInlineFunctionDef(FD);
593
609k
}
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
611k
void Parser::ParseLexedMemberInitializers(ParsingClass &Class) {
599
611k
  ReenterClassScopeRAII InClassScope(*this, Class);
600
601
611k
  if (!Class.LateParsedDeclarations.empty()) {
602
    // C++11 [expr.prim.general]p4:
603
    //   Otherwise, if a member-declarator declares a non-static data member
604
    //  (9.2) of a class X, the expression this is a prvalue of type "pointer
605
    //  to X" within the optional brace-or-equal-initializer. It shall not
606
    //  appear elsewhere in the member-declarator.
607
    // FIXME: This should be done in ParseLexedMemberInitializer, not here.
608
138k
    Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate,
609
138k
                                     Qualifiers());
610
611
138k
    for (LateParsedDeclaration *D : Class.LateParsedDeclarations)
612
715k
      D->ParseLexedMemberInitializers();
613
138k
  }
614
615
611k
  Actions.ActOnFinishDelayedMemberInitializers(Class.TagOrTemplate);
616
611k
}
617
618
2.96k
void Parser::ParseLexedMemberInitializer(LateParsedMemberInitializer &MI) {
619
2.96k
  if (!MI.Field || 
MI.Field->isInvalidDecl()2.94k
)
620
24
    return;
621
622
2.93k
  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
2.93k
  MI.Toks.push_back(Tok);
627
2.93k
  PP.EnterTokenStream(MI.Toks, true, /*IsReinject*/true);
628
629
  // Consume the previously pushed token.
630
2.93k
  ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
631
632
2.93k
  SourceLocation EqualLoc;
633
634
2.93k
  Actions.ActOnStartCXXInClassMemberInitializer();
635
636
2.93k
  ExprResult Init = ParseCXXMemberInitializer(MI.Field, /*IsFunction=*/false,
637
2.93k
                                              EqualLoc);
638
639
2.93k
  Actions.ActOnFinishCXXInClassMemberInitializer(MI.Field, EqualLoc,
640
2.93k
                                                 Init.get());
641
642
  // The next token should be our artificial terminating EOF token.
643
2.93k
  if (Tok.isNot(tok::eof)) {
644
19
    if (!Init.isInvalid()) {
645
8
      SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
646
8
      if (!EndLoc.isValid())
647
0
        EndLoc = Tok.getLocation();
648
      // No fixit; we can't recover as if there were a semicolon here.
649
8
      Diag(EndLoc, diag::err_expected_semi_decl_list);
650
8
    }
651
652
    // Consume tokens until we hit the artificial EOF.
653
46
    while (Tok.isNot(tok::eof))
654
27
      ConsumeAnyToken();
655
19
  }
656
  // Make sure this is *our* artificial EOF token.
657
2.93k
  if (Tok.getEofData() == MI.Field)
658
2.93k
    ConsumeAnyToken();
659
2.93k
}
660
661
/// Wrapper class which calls ParseLexedAttribute, after setting up the
662
/// scope appropriately.
663
611k
void Parser::ParseLexedAttributes(ParsingClass &Class) {
664
611k
  ReenterClassScopeRAII InClassScope(*this, Class);
665
666
611k
  for (LateParsedDeclaration *LateD : Class.LateParsedDeclarations)
667
715k
    LateD->ParseLexedAttributes();
668
611k
}
669
670
/// Parse all attributes in LAs, and attach them to Decl D.
671
void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
672
2.61M
                                     bool EnterScope, bool OnDefinition) {
673
2.61M
  assert(LAs.parseSoon() &&
674
2.61M
         "Attribute list should be marked for immediate parsing.");
675
2.61M
  for (unsigned i = 0, ni = LAs.size(); i < ni; 
++i4.95k
) {
676
4.95k
    if (D)
677
4.94k
      LAs[i]->addDecl(D);
678
4.95k
    ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
679
4.95k
    delete LAs[i];
680
4.95k
  }
681
2.61M
  LAs.clear();
682
2.61M
}
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
10.1k
                                 bool EnterScope, bool OnDefinition) {
691
  // Create a fake EOF so that attribute parsing won't go off the end of the
692
  // attribute.
693
10.1k
  Token AttrEnd;
694
10.1k
  AttrEnd.startToken();
695
10.1k
  AttrEnd.setKind(tok::eof);
696
10.1k
  AttrEnd.setLocation(Tok.getLocation());
697
10.1k
  AttrEnd.setEofData(LA.Toks.data());
698
10.1k
  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
10.1k
  LA.Toks.push_back(Tok);
703
10.1k
  PP.EnterTokenStream(LA.Toks, true, /*IsReinject=*/true);
704
  // Consume the previously pushed token.
705
10.1k
  ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
706
707
10.1k
  ParsedAttributes Attrs(AttrFactory);
708
10.1k
  SourceLocation endLoc;
709
710
10.1k
  if (LA.Decls.size() > 0) {
711
10.1k
    Decl *D = LA.Decls[0];
712
10.1k
    NamedDecl *ND  = dyn_cast<NamedDecl>(D);
713
10.1k
    RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
714
715
    // Allow 'this' within late-parsed attributes.
716
10.1k
    Sema::CXXThisScopeRAII ThisScope(Actions, RD, Qualifiers(),
717
10.1k
                                     ND && ND->isCXXInstanceMember());
718
719
10.1k
    if (LA.Decls.size() == 1) {
720
      // If the Decl is templatized, add template parameters to scope.
721
10.1k
      ReenterTemplateScopeRAII InDeclScope(*this, D, EnterScope);
722
723
      // If the Decl is on a function, add function parameters to the scope.
724
10.1k
      bool HasFunScope = EnterScope && 
D->isFunctionOrFunctionTemplate()5.89k
;
725
10.1k
      if (HasFunScope) {
726
5.23k
        InDeclScope.Scopes.Enter(Scope::FnScope | Scope::DeclScope |
727
5.23k
                                 Scope::CompoundStmtScope);
728
5.23k
        Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
729
5.23k
      }
730
731
10.1k
      ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
732
10.1k
                            nullptr, SourceLocation(), ParsedAttr::AS_GNU,
733
10.1k
                            nullptr);
734
735
10.1k
      if (HasFunScope)
736
5.23k
        Actions.ActOnExitFunctionContext();
737
7
    } else {
738
      // If there are multiple decls, then the decl cannot be within the
739
      // function scope.
740
7
      ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
741
7
                            nullptr, SourceLocation(), ParsedAttr::AS_GNU,
742
7
                            nullptr);
743
7
    }
744
8
  } else {
745
8
    Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
746
8
  }
747
748
10.1k
  if (OnDefinition && 
!Attrs.empty()4.27k
&&
!Attrs.begin()->isCXX11Attribute()4.27k
&&
749
4.27k
      Attrs.begin()->isKnownToGCC())
750
0
    Diag(Tok, diag::warn_attribute_on_function_definition)
751
0
      << &LA.AttrName;
752
753
20.3k
  for (unsigned i = 0, ni = LA.Decls.size(); i < ni; 
++i10.1k
)
754
10.1k
    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
20.3k
  while (Tok.isNot(tok::eof))
759
10.2k
    ConsumeAnyToken();
760
761
10.1k
  if (Tok.is(tok::eof) && Tok.getEofData() == AttrEnd.getEofData())
762
10.1k
    ConsumeAnyToken();
763
10.1k
}
764
765
611k
void Parser::ParseLexedPragmas(ParsingClass &Class) {
766
611k
  ReenterClassScopeRAII InClassScope(*this, Class);
767
768
611k
  for (LateParsedDeclaration *D : Class.LateParsedDeclarations)
769
715k
    D->ParseLexedPragmas();
770
611k
}
771
772
86
void Parser::ParseLexedPragma(LateParsedPragma &LP) {
773
86
  PP.EnterToken(Tok, /*IsReinject=*/true);
774
86
  PP.EnterTokenStream(LP.toks(), /*DisableMacroExpansion=*/true,
775
86
                      /*IsReinject=*/true);
776
777
  // Consume the previously pushed token.
778
86
  ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
779
86
  assert(Tok.isAnnotation() && "Expected annotation token.");
780
86
  switch (Tok.getKind()) {
781
86
  case tok::annot_pragma_openmp: {
782
86
    AccessSpecifier AS = LP.getAccessSpecifier();
783
86
    ParsedAttributesWithRange Attrs(AttrFactory);
784
86
    (void)ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs);
785
86
    break;
786
0
  }
787
0
  default:
788
0
    llvm_unreachable("Unexpected token.");
789
86
  }
790
86
}
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
2.25M
                                  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
2.25M
  bool isFirstTokenConsumed = true;
804
10.0M
  while (1) {
805
    // If we found one of the tokens, stop and return true.
806
10.0M
    if (Tok.is(T1) || 
Tok.is(T2)7.79M
) {
807
2.25M
      if (ConsumeFinalToken) {
808
1.75M
        Toks.push_back(Tok);
809
1.75M
        ConsumeAnyToken();
810
1.75M
      }
811
2.25M
      return true;
812
2.25M
    }
813
814
7.79M
    switch (Tok.getKind()) {
815
154
    case tok::eof:
816
154
    case tok::annot_module_begin:
817
154
    case tok::annot_module_end:
818
154
    case tok::annot_module_include:
819
      // Ran out of tokens.
820
154
      return false;
821
822
883k
    case tok::l_paren:
823
      // Recursively consume properly-nested parens.
824
883k
      Toks.push_back(Tok);
825
883k
      ConsumeParen();
826
883k
      ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
827
883k
      break;
828
20.9k
    case tok::l_square:
829
      // Recursively consume properly-nested square brackets.
830
20.9k
      Toks.push_back(Tok);
831
20.9k
      ConsumeBracket();
832
20.9k
      ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
833
20.9k
      break;
834
31.5k
    case tok::l_brace:
835
      // Recursively consume properly-nested braces.
836
31.5k
      Toks.push_back(Tok);
837
31.5k
      ConsumeBrace();
838
31.5k
      ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
839
31.5k
      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
5
    case tok::r_paren:
847
5
      if (ParenCount && !isFirstTokenConsumed)
848
4
        return false;  // Matches something.
849
1
      Toks.push_back(Tok);
850
1
      ConsumeParen();
851
1
      break;
852
33
    case tok::r_square:
853
33
      if (BracketCount && 
!isFirstTokenConsumed0
)
854
0
        return false;  // Matches something.
855
33
      Toks.push_back(Tok);
856
33
      ConsumeBracket();
857
33
      break;
858
89
    case tok::r_brace:
859
89
      if (BraceCount && !isFirstTokenConsumed)
860
89
        return false;  // Matches something.
861
0
      Toks.push_back(Tok);
862
0
      ConsumeBrace();
863
0
      break;
864
865
733k
    case tok::semi:
866
733k
      if (StopAtSemi)
867
10
        return false;
868
733k
      LLVM_FALLTHROUGH;
869
6.85M
    default:
870
      // consume this token.
871
6.85M
      Toks.push_back(Tok);
872
6.85M
      ConsumeAnyToken(/*ConsumeCodeCompletionTok*/true);
873
6.85M
      break;
874
7.79M
    }
875
7.79M
    isFirstTokenConsumed = false;
876
7.79M
  }
877
2.25M
}
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
613k
bool Parser::ConsumeAndStoreFunctionPrologue(CachedTokens &Toks) {
886
613k
  if (Tok.is(tok::kw_try)) {
887
75
    Toks.push_back(Tok);
888
75
    ConsumeToken();
889
75
  }
890
891
613k
  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
496k
    ConsumeAndStoreUntil(tok::l_brace, tok::r_brace, Toks,
898
496k
                         /*StopAtSemi=*/true,
899
496k
                         /*ConsumeFinalToken=*/false);
900
496k
    if (Tok.isNot(tok::l_brace))
901
1
      return Diag(Tok.getLocation(), diag::err_expected) << tok::l_brace;
902
903
496k
    Toks.push_back(Tok);
904
496k
    ConsumeBrace();
905
496k
    return false;
906
496k
  }
907
908
116k
  Toks.push_back(Tok);
909
116k
  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
116k
  bool MightBeTemplateArgument = false;
922
923
153k
  while (true) {
924
    // Skip over the mem-initializer-id, if possible.
925
151k
    if (Tok.is(tok::kw_decltype)) {
926
8
      Toks.push_back(Tok);
927
8
      SourceLocation OpenLoc = ConsumeToken();
928
8
      if (Tok.isNot(tok::l_paren))
929
1
        return Diag(Tok.getLocation(), diag::err_expected_lparen_after)
930
1
                 << "decltype";
931
7
      Toks.push_back(Tok);
932
7
      ConsumeParen();
933
7
      if (!ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/true)) {
934
1
        Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
935
1
        Diag(OpenLoc, diag::note_matching) << tok::l_paren;
936
1
        return true;
937
1
      }
938
151k
    }
939
153k
    
do 151k
{
940
      // Walk over a component of a nested-name-specifier.
941
153k
      if (Tok.is(tok::coloncolon)) {
942
1.58k
        Toks.push_back(Tok);
943
1.58k
        ConsumeToken();
944
945
1.58k
        if (Tok.is(tok::kw_template)) {
946
2
          Toks.push_back(Tok);
947
2
          ConsumeToken();
948
2
        }
949
1.58k
      }
950
951
153k
      if (Tok.is(tok::identifier)) {
952
153k
        Toks.push_back(Tok);
953
153k
        ConsumeToken();
954
203
      } else {
955
203
        break;
956
203
      }
957
153k
    } while (Tok.is(tok::coloncolon));
958
959
153k
    if (Tok.is(tok::code_completion)) {
960
47
      Toks.push_back(Tok);
961
47
      ConsumeCodeCompletionToken();
962
47
      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
21
        continue;
966
21
      }
967
153k
    }
968
969
153k
    if (Tok.is(tok::comma)) {
970
      // The initialization is missing, we'll diagnose it later.
971
19
      Toks.push_back(Tok);
972
19
      ConsumeToken();
973
19
      continue;
974
19
    }
975
152k
    if (Tok.is(tok::less))
976
4.41k
      MightBeTemplateArgument = true;
977
978
152k
    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
4.83k
      if (!ConsumeAndStoreUntil(tok::l_paren, tok::l_brace, Toks,
986
4.83k
                                /*StopAtSemi=*/true,
987
0
                                /*ConsumeFinalToken=*/false)) {
988
        // We're not just missing the initializer, we're also missing the
989
        // function body!
990
0
        return Diag(Tok.getLocation(), diag::err_expected) << tok::l_brace;
991
0
      }
992
148k
    } else if (Tok.isNot(tok::l_paren) && 
Tok.isNot(tok::l_brace)308
) {
993
      // We found something weird in a mem-initializer-id.
994
1
      if (getLangOpts().CPlusPlus11)
995
1
        return Diag(Tok.getLocation(), diag::err_expected_either)
996
1
               << tok::l_paren << tok::l_brace;
997
0
      else
998
0
        return Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
999
152k
    }
1000
1001
152k
    tok::TokenKind kind = Tok.getKind();
1002
152k
    Toks.push_back(Tok);
1003
152k
    bool IsLParen = (kind == tok::l_paren);
1004
152k
    SourceLocation OpenLoc = Tok.getLocation();
1005
1006
152k
    if (IsLParen) {
1007
151k
      ConsumeParen();
1008
1.68k
    } else {
1009
1.68k
      assert(kind == tok::l_brace && "Must be left paren or brace here.");
1010
1.68k
      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
1.68k
      if (!getLangOpts().CPlusPlus11)
1014
63
        return false;
1015
1016
1.61k
      const Token &PreviousToken = Toks[Toks.size() - 2];
1017
1.61k
      if (!MightBeTemplateArgument &&
1018
244
          !PreviousToken.isOneOf(tok::identifier, tok::greater,
1019
79
                                 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
79
        TentativeParsingAction PA(*this);
1026
79
        if (SkipUntil(tok::r_brace) &&
1027
79
            !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
79
          PA.Revert();
1031
79
          return false;
1032
79
        }
1033
0
        PA.Revert();
1034
0
      }
1035
1.61k
    }
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
152k
    tok::TokenKind CloseKind = IsLParen ? 
tok::r_paren151k
:
tok::r_brace1.53k
;
1041
152k
    if (!ConsumeAndStoreUntil(CloseKind, Toks, /*StopAtSemi=*/true)) {
1042
5
      Diag(Tok, diag::err_expected) << CloseKind;
1043
5
      Diag(OpenLoc, diag::note_matching) << kind;
1044
5
      return true;
1045
5
    }
1046
1047
    // Grab pack ellipsis, if present.
1048
152k
    if (Tok.is(tok::ellipsis)) {
1049
2.45k
      Toks.push_back(Tok);
1050
2.45k
      ConsumeToken();
1051
2.45k
    }
1052
1053
    // If we know we just consumed a mem-initializer, we must have ',' or '{'
1054
    // next.
1055
152k
    if (Tok.is(tok::comma)) {
1056
34.8k
      Toks.push_back(Tok);
1057
34.8k
      ConsumeToken();
1058
117k
    } 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
116k
      Toks.push_back(Tok);
1074
116k
      ConsumeBrace();
1075
116k
      return false;
1076
1.38k
    } else if (!MightBeTemplateArgument) {
1077
8
      return Diag(Tok.getLocation(), diag::err_expected_either) << tok::l_brace
1078
8
                                                                << tok::comma;
1079
8
    }
1080
152k
  }
1081
116k
}
1082
1083
/// Consume and store tokens from the '?' to the ':' in a conditional
1084
/// expression.
1085
4
bool Parser::ConsumeAndStoreConditional(CachedTokens &Toks) {
1086
  // Consume '?'.
1087
4
  assert(Tok.is(tok::question));
1088
4
  Toks.push_back(Tok);
1089
4
  ConsumeToken();
1090
1091
8
  while (Tok.isNot(tok::colon)) {
1092
4
    if (!ConsumeAndStoreUntil(tok::question, tok::colon, Toks,
1093
4
                              /*StopAtSemi=*/true,
1094
4
                              /*ConsumeFinalToken=*/false))
1095
0
      return false;
1096
1097
    // If we found a nested conditional, consume it.
1098
4
    if (Tok.is(tok::question) && 
!ConsumeAndStoreConditional(Toks)0
)
1099
0
      return false;
1100
4
  }
1101
1102
  // Consume ':'.
1103
4
  Toks.push_back(Tok);
1104
4
  ConsumeToken();
1105
4
  return true;
1106
4
}
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
84
      : 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
84
    TentativeParsingAction Inner(Self);
1117
84
    Self.ConsumeAndStoreUntil(EndKind, Toks, true, /*ConsumeFinalToken*/false);
1118
84
    Inner.Revert();
1119
84
  }
1120
1121
84
  void RevertAnnotations() {
1122
84
    Revert();
1123
1124
    // Put back the original tokens.
1125
84
    Self.SkipUntil(EndKind, StopAtSemi | StopBeforeMatch);
1126
84
    if (Toks.size()) {
1127
84
      auto Buffer = std::make_unique<Token[]>(Toks.size());
1128
84
      std::copy(Toks.begin() + 1, Toks.end(), Buffer.get());
1129
84
      Buffer[Toks.size() - 1] = Self.Tok;
1130
84
      Self.PP.EnterTokenStream(std::move(Buffer), Toks.size(), true,
1131
84
                               /*IsReinject*/ true);
1132
1133
84
      Self.Tok = Toks.front();
1134
84
    }
1135
84
  }
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
68.2k
                                        CachedInitKind CIK) {
1151
  // We always want this function to consume at least one token if not at EOF.
1152
68.2k
  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
68.2k
  unsigned AngleCount = 0;
1158
68.2k
  unsigned KnownTemplateCount = 0;
1159
1160
225k
  while (1) {
1161
225k
    switch (Tok.getKind()) {
1162
5.23k
    case tok::comma:
1163
      // If we might be in a template, perform a tentative parse to check.
1164
5.23k
      if (!AngleCount)
1165
        // Not a template argument: this is the end of the initializer.
1166
5.13k
        return true;
1167
97
      if (KnownTemplateCount)
1168
13
        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
84
      {
1180
84
        UnannotatedTentativeParsingAction PA(*this,
1181
84
                                             CIK == CIK_DefaultInitializer
1182
48
                                               ? 
tok::semi36
: tok::r_paren);
1183
84
        Sema::TentativeAnalysisScope Scope(Actions);
1184
1185
84
        TPResult Result = TPResult::Error;
1186
84
        ConsumeToken();
1187
84
        switch (CIK) {
1188
36
        case CIK_DefaultInitializer:
1189
36
          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
36
          if (Result == TPResult::Ambiguous && 
Tok.isNot(tok::semi)21
)
1193
13
            Result = TPResult::False;
1194
36
          break;
1195
1196
48
        case CIK_DefaultArgument:
1197
48
          bool InvalidAsDeclaration = false;
1198
48
          Result = TryParseParameterDeclarationClause(
1199
48
              &InvalidAsDeclaration, /*VersusTemplateArg=*/true);
1200
          // If this is an expression or a declaration with a missing
1201
          // 'typename', assume it's not a declaration.
1202
48
          if (Result == TPResult::Ambiguous && 
InvalidAsDeclaration0
)
1203
0
            Result = TPResult::False;
1204
48
          break;
1205
84
        }
1206
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
84
        PA.RevertAnnotations();
1211
1212
        // If what follows could be a declaration, it is a declaration.
1213
84
        if (Result != TPResult::False && 
Result != TPResult::Error28
)
1214
26
          return true;
1215
58
      }
1216
1217
      // Keep going. We know we're inside a template argument list now.
1218
58
      ++KnownTemplateCount;
1219
58
      goto consume_token;
1220
1221
1
    case tok::eof:
1222
1
    case tok::annot_module_begin:
1223
1
    case tok::annot_module_end:
1224
1
    case tok::annot_module_include:
1225
      // Ran out of tokens.
1226
1
      return false;
1227
1228
1.00k
    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
1.00k
      ++AngleCount;
1232
1.00k
      goto consume_token;
1233
1234
4
    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
4
      if (!ConsumeAndStoreConditional(Toks))
1238
0
        return false;
1239
4
      break;
1240
1241
0
    case tok::greatergreatergreater:
1242
0
      if (!getLangOpts().CPlusPlus11)
1243
0
        goto consume_token;
1244
0
      if (AngleCount) --AngleCount;
1245
0
      if (KnownTemplateCount) --KnownTemplateCount;
1246
0
      LLVM_FALLTHROUGH;
1247
7
    case tok::greatergreater:
1248
7
      if (!getLangOpts().CPlusPlus11)
1249
0
        goto consume_token;
1250
7
      if (AngleCount) 
--AngleCount6
;
1251
7
      if (KnownTemplateCount) 
--KnownTemplateCount6
;
1252
7
      LLVM_FALLTHROUGH;
1253
990
    case tok::greater:
1254
990
      if (AngleCount) 
--AngleCount980
;
1255
990
      if (KnownTemplateCount) 
--KnownTemplateCount65
;
1256
990
      goto consume_token;
1257
1258
14
    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
14
      Toks.push_back(Tok);
1263
14
      ConsumeToken();
1264
14
      if (Tok.is(tok::identifier)) {
1265
14
        Toks.push_back(Tok);
1266
14
        ConsumeToken();
1267
14
        if (Tok.is(tok::less)) {
1268
14
          ++AngleCount;
1269
14
          ++KnownTemplateCount;
1270
14
          Toks.push_back(Tok);
1271
14
          ConsumeToken();
1272
14
        }
1273
14
      }
1274
14
      break;
1275
1276
20
    case tok::kw_operator:
1277
      // If 'operator' precedes other punctuation, that punctuation loses
1278
      // its special behavior.
1279
20
      Toks.push_back(Tok);
1280
20
      ConsumeToken();
1281
20
      switch (Tok.getKind()) {
1282
6
      case tok::comma:
1283
6
      case tok::greatergreatergreater:
1284
8
      case tok::greatergreater:
1285
14
      case tok::greater:
1286
19
      case tok::less:
1287
19
        Toks.push_back(Tok);
1288
19
        ConsumeToken();
1289
19
        break;
1290
1
      default:
1291
1
        break;
1292
20
      }
1293
20
      break;
1294
1295
10.1k
    case tok::l_paren:
1296
      // Recursively consume properly-nested parens.
1297
10.1k
      Toks.push_back(Tok);
1298
10.1k
      ConsumeParen();
1299
10.1k
      ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
1300
10.1k
      break;
1301
262
    case tok::l_square:
1302
      // Recursively consume properly-nested square brackets.
1303
262
      Toks.push_back(Tok);
1304
262
      ConsumeBracket();
1305
262
      ConsumeAndStoreUntil(tok::r_square, Toks, /*StopAtSemi=*/false);
1306
262
      break;
1307
924
    case tok::l_brace:
1308
      // Recursively consume properly-nested braces.
1309
924
      Toks.push_back(Tok);
1310
924
      ConsumeBrace();
1311
924
      ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
1312
924
      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
60.2k
    case tok::r_paren:
1321
60.2k
      if (CIK == CIK_DefaultArgument)
1322
60.2k
        return true; // End of the default argument.
1323
6
      if (ParenCount && 
!IsFirstToken0
)
1324
0
        return false;
1325
6
      Toks.push_back(Tok);
1326
6
      ConsumeParen();
1327
6
      continue;
1328
6
    case tok::r_square:
1329
6
      if (BracketCount && 
!IsFirstToken0
)
1330
0
        return false;
1331
6
      Toks.push_back(Tok);
1332
6
      ConsumeBracket();
1333
6
      continue;
1334
6
    case tok::r_brace:
1335
6
      if (BraceCount && !IsFirstToken)
1336
3
        return false;
1337
3
      Toks.push_back(Tok);
1338
3
      ConsumeBrace();
1339
3
      continue;
1340
1341
0
    case tok::code_completion:
1342
0
      Toks.push_back(Tok);
1343
0
      ConsumeCodeCompletionToken();
1344
0
      break;
1345
1346
34
    case tok::string_literal:
1347
34
    case tok::wide_string_literal:
1348
34
    case tok::utf8_string_literal:
1349
34
    case tok::utf16_string_literal:
1350
34
    case tok::utf32_string_literal:
1351
34
      Toks.push_back(Tok);
1352
34
      ConsumeStringToken();
1353
34
      break;
1354
2.82k
    case tok::semi:
1355
2.82k
      if (CIK == CIK_DefaultInitializer)
1356
2.82k
        return true; // End of the default initializer.
1357
0
      LLVM_FALLTHROUGH;
1358
143k
    default:
1359
145k
    consume_token:
1360
145k
      Toks.push_back(Tok);
1361
145k
      ConsumeToken();
1362
145k
      break;
1363
157k
    }
1364
157k
    IsFirstToken = false;
1365
157k
  }
1366
68.2k
}