Coverage Report

Created: 2020-10-24 06:27

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