Coverage Report

Created: 2020-02-15 09:57

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Parse/ParseTentative.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- ParseTentative.cpp - Ambiguity Resolution 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 the tentative parsing portions of the Parser
10
//  interfaces, for ambiguity resolution.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "clang/Parse/Parser.h"
15
#include "clang/Parse/ParseDiagnostic.h"
16
#include "clang/Sema/ParsedTemplate.h"
17
using namespace clang;
18
19
/// isCXXDeclarationStatement - C++-specialized function that disambiguates
20
/// between a declaration or an expression statement, when parsing function
21
/// bodies. Returns true for declaration, false for expression.
22
///
23
///         declaration-statement:
24
///           block-declaration
25
///
26
///         block-declaration:
27
///           simple-declaration
28
///           asm-definition
29
///           namespace-alias-definition
30
///           using-declaration
31
///           using-directive
32
/// [C++0x]   static_assert-declaration
33
///
34
///         asm-definition:
35
///           'asm' '(' string-literal ')' ';'
36
///
37
///         namespace-alias-definition:
38
///           'namespace' identifier = qualified-namespace-specifier ';'
39
///
40
///         using-declaration:
41
///           'using' typename[opt] '::'[opt] nested-name-specifier
42
///                 unqualified-id ';'
43
///           'using' '::' unqualified-id ;
44
///
45
///         using-directive:
46
///           'using' 'namespace' '::'[opt] nested-name-specifier[opt]
47
///                 namespace-name ';'
48
///
49
1.88M
bool Parser::isCXXDeclarationStatement() {
50
1.88M
  switch (Tok.getKind()) {
51
0
    // asm-definition
52
39.5k
  case tok::kw_asm:
53
39.5k
    // namespace-alias-definition
54
39.5k
  case tok::kw_namespace:
55
39.5k
    // using-declaration
56
39.5k
    // using-directive
57
39.5k
  case tok::kw_using:
58
39.5k
    // static_assert-declaration
59
39.5k
  case tok::kw_static_assert:
60
39.5k
  case tok::kw__Static_assert:
61
39.5k
    return true;
62
39.5k
    // simple-declaration
63
1.84M
  default:
64
1.84M
    return isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false);
65
1.88M
  }
66
1.88M
}
67
68
/// isCXXSimpleDeclaration - C++-specialized function that disambiguates
69
/// between a simple-declaration or an expression-statement.
70
/// If during the disambiguation process a parsing error is encountered,
71
/// the function returns true to let the declaration parsing code handle it.
72
/// Returns false if the statement is disambiguated as expression.
73
///
74
/// simple-declaration:
75
///   decl-specifier-seq init-declarator-list[opt] ';'
76
///   decl-specifier-seq ref-qualifier[opt] '[' identifier-list ']'
77
///                      brace-or-equal-initializer ';'    [C++17]
78
///
79
/// (if AllowForRangeDecl specified)
80
/// for ( for-range-declaration : for-range-initializer ) statement
81
///
82
/// for-range-declaration:
83
///    decl-specifier-seq declarator
84
///    decl-specifier-seq ref-qualifier[opt] '[' identifier-list ']'
85
///
86
/// In any of the above cases there can be a preceding attribute-specifier-seq,
87
/// but the caller is expected to handle that.
88
1.95M
bool Parser::isCXXSimpleDeclaration(bool AllowForRangeDecl) {
89
1.95M
  // C++ 6.8p1:
90
1.95M
  // There is an ambiguity in the grammar involving expression-statements and
91
1.95M
  // declarations: An expression-statement with a function-style explicit type
92
1.95M
  // conversion (5.2.3) as its leftmost subexpression can be indistinguishable
93
1.95M
  // from a declaration where the first declarator starts with a '('. In those
94
1.95M
  // cases the statement is a declaration. [Note: To disambiguate, the whole
95
1.95M
  // statement might have to be examined to determine if it is an
96
1.95M
  // expression-statement or a declaration].
97
1.95M
98
1.95M
  // C++ 6.8p3:
99
1.95M
  // The disambiguation is purely syntactic; that is, the meaning of the names
100
1.95M
  // occurring in such a statement, beyond whether they are type-names or not,
101
1.95M
  // is not generally used in or changed by the disambiguation. Class
102
1.95M
  // templates are instantiated as necessary to determine if a qualified name
103
1.95M
  // is a type-name. Disambiguation precedes parsing, and a statement
104
1.95M
  // disambiguated as a declaration may be an ill-formed declaration.
105
1.95M
106
1.95M
  // We don't have to parse all of the decl-specifier-seq part. There's only
107
1.95M
  // an ambiguity if the first decl-specifier is
108
1.95M
  // simple-type-specifier/typename-specifier followed by a '(', which may
109
1.95M
  // indicate a function-style cast expression.
110
1.95M
  // isCXXDeclarationSpecifier will return TPResult::Ambiguous only in such
111
1.95M
  // a case.
112
1.95M
113
1.95M
  bool InvalidAsDeclaration = false;
114
1.95M
  TPResult TPR = isCXXDeclarationSpecifier(TPResult::False,
115
1.95M
                                           &InvalidAsDeclaration);
116
1.95M
  if (TPR != TPResult::Ambiguous)
117
1.90M
    return TPR != TPResult::False; // Returns true for TPResult::True or
118
50.2k
                                   // TPResult::Error.
119
50.2k
120
50.2k
  // FIXME: TryParseSimpleDeclaration doesn't look past the first initializer,
121
50.2k
  // and so gets some cases wrong. We can't carry on if we've already seen
122
50.2k
  // something which makes this statement invalid as a declaration in this case,
123
50.2k
  // since it can cause us to misparse valid code. Revisit this once
124
50.2k
  // TryParseInitDeclaratorList is fixed.
125
50.2k
  if (InvalidAsDeclaration)
126
38.2k
    return false;
127
11.9k
128
11.9k
  // FIXME: Add statistics about the number of ambiguous statements encountered
129
11.9k
  // and how they were resolved (number of declarations+number of expressions).
130
11.9k
131
11.9k
  // Ok, we have a simple-type-specifier/typename-specifier followed by a '(',
132
11.9k
  // or an identifier which doesn't resolve as anything. We need tentative
133
11.9k
  // parsing...
134
11.9k
135
11.9k
  {
136
11.9k
    RevertingTentativeParsingAction PA(*this);
137
11.9k
    TPR = TryParseSimpleDeclaration(AllowForRangeDecl);
138
11.9k
  }
139
11.9k
140
11.9k
  // In case of an error, let the declaration parsing code handle it.
141
11.9k
  if (TPR == TPResult::Error)
142
0
    return true;
143
11.9k
144
11.9k
  // Declarations take precedence over expressions.
145
11.9k
  if (TPR == TPResult::Ambiguous)
146
286
    TPR = TPResult::True;
147
11.9k
148
11.9k
  assert(TPR == TPResult::True || TPR == TPResult::False);
149
11.9k
  return TPR == TPResult::True;
150
11.9k
}
151
152
/// Try to consume a token sequence that we've already identified as
153
/// (potentially) starting a decl-specifier.
154
76.9k
Parser::TPResult Parser::TryConsumeDeclarationSpecifier() {
155
76.9k
  switch (Tok.getKind()) {
156
0
  case tok::kw__Atomic:
157
0
    if (NextToken().isNot(tok::l_paren)) {
158
0
      ConsumeToken();
159
0
      break;
160
0
    }
161
0
    LLVM_FALLTHROUGH;
162
14
  case tok::kw_typeof:
163
14
  case tok::kw___attribute:
164
14
  case tok::kw___underlying_type: {
165
14
    ConsumeToken();
166
14
    if (Tok.isNot(tok::l_paren))
167
0
      return TPResult::Error;
168
14
    ConsumeParen();
169
14
    if (!SkipUntil(tok::r_paren))
170
0
      return TPResult::Error;
171
14
    break;
172
14
  }
173
14
174
14
  case tok::kw_class:
175
5
  case tok::kw_struct:
176
5
  case tok::kw_union:
177
5
  case tok::kw___interface:
178
5
  case tok::kw_enum:
179
5
    // elaborated-type-specifier:
180
5
    //     class-key attribute-specifier-seq[opt]
181
5
    //         nested-name-specifier[opt] identifier
182
5
    //     class-key nested-name-specifier[opt] template[opt] simple-template-id
183
5
    //     enum nested-name-specifier[opt] identifier
184
5
    //
185
5
    // FIXME: We don't support class-specifiers nor enum-specifiers here.
186
5
    ConsumeToken();
187
5
188
5
    // Skip attributes.
189
6
    while (Tok.isOneOf(tok::l_square, tok::kw___attribute, tok::kw___declspec,
190
6
                       tok::kw_alignas)) {
191
1
      if (Tok.is(tok::l_square)) {
192
0
        ConsumeBracket();
193
0
        if (!SkipUntil(tok::r_square))
194
0
          return TPResult::Error;
195
1
      } else {
196
1
        ConsumeToken();
197
1
        if (Tok.isNot(tok::l_paren))
198
0
          return TPResult::Error;
199
1
        ConsumeParen();
200
1
        if (!SkipUntil(tok::r_paren))
201
0
          return TPResult::Error;
202
1
      }
203
1
    }
204
5
205
5
    if (TryAnnotateOptionalCXXScopeToken())
206
1
      return TPResult::Error;
207
4
    if (Tok.is(tok::annot_cxxscope))
208
0
      ConsumeAnnotationToken();
209
4
    if (Tok.is(tok::identifier))
210
3
      ConsumeToken();
211
1
    else if (Tok.is(tok::annot_template_id))
212
0
      ConsumeAnnotationToken();
213
1
    else
214
1
      return TPResult::Error;
215
3
    break;
216
3
217
597
  case tok::annot_cxxscope:
218
597
    ConsumeAnnotationToken();
219
597
    LLVM_FALLTHROUGH;
220
76.9k
  default:
221
76.9k
    ConsumeAnyToken();
222
76.9k
223
76.9k
    if (getLangOpts().ObjC && 
Tok.is(tok::less)2.76k
)
224
0
      return TryParseProtocolQualifiers();
225
76.9k
    break;
226
76.9k
  }
227
76.9k
228
76.9k
  return TPResult::Ambiguous;
229
76.9k
}
230
231
/// simple-declaration:
232
///   decl-specifier-seq init-declarator-list[opt] ';'
233
///
234
/// (if AllowForRangeDecl specified)
235
/// for ( for-range-declaration : for-range-initializer ) statement
236
/// for-range-declaration:
237
///    attribute-specifier-seqopt type-specifier-seq declarator
238
///
239
11.9k
Parser::TPResult Parser::TryParseSimpleDeclaration(bool AllowForRangeDecl) {
240
11.9k
  if (TryConsumeDeclarationSpecifier() == TPResult::Error)
241
0
    return TPResult::Error;
242
11.9k
243
11.9k
  // Two decl-specifiers in a row conclusively disambiguate this as being a
244
11.9k
  // simple-declaration. Don't bother calling isCXXDeclarationSpecifier in the
245
11.9k
  // overwhelmingly common case that the next token is a '('.
246
11.9k
  if (Tok.isNot(tok::l_paren)) {
247
635
    TPResult TPR = isCXXDeclarationSpecifier();
248
635
    if (TPR == TPResult::Ambiguous)
249
0
      return TPResult::True;
250
635
    if (TPR == TPResult::True || TPR == TPResult::Error)
251
0
      return TPR;
252
635
    assert(TPR == TPResult::False);
253
635
  }
254
11.9k
255
11.9k
  TPResult TPR = TryParseInitDeclaratorList();
256
11.9k
  if (TPR != TPResult::Ambiguous)
257
9.05k
    return TPR;
258
2.90k
259
2.90k
  if (Tok.isNot(tok::semi) && 
(2.62k
!AllowForRangeDecl2.62k
||
Tok.isNot(tok::colon)8
))
260
2.62k
    return TPResult::False;
261
286
262
286
  return TPResult::Ambiguous;
263
286
}
264
265
/// Tentatively parse an init-declarator-list in order to disambiguate it from
266
/// an expression.
267
///
268
///       init-declarator-list:
269
///         init-declarator
270
///         init-declarator-list ',' init-declarator
271
///
272
///       init-declarator:
273
///         declarator initializer[opt]
274
/// [GNU]   declarator simple-asm-expr[opt] attributes[opt] initializer[opt]
275
///
276
///       initializer:
277
///         brace-or-equal-initializer
278
///         '(' expression-list ')'
279
///
280
///       brace-or-equal-initializer:
281
///         '=' initializer-clause
282
/// [C++11] braced-init-list
283
///
284
///       initializer-clause:
285
///         assignment-expression
286
///         braced-init-list
287
///
288
///       braced-init-list:
289
///         '{' initializer-list ','[opt] '}'
290
///         '{' '}'
291
///
292
11.9k
Parser::TPResult Parser::TryParseInitDeclaratorList() {
293
12.0k
  while (1) {
294
12.0k
    // declarator
295
12.0k
    TPResult TPR = TryParseDeclarator(false/*mayBeAbstract*/);
296
12.0k
    if (TPR != TPResult::Ambiguous)
297
7.51k
      return TPR;
298
4.49k
299
4.49k
    // [GNU] simple-asm-expr[opt] attributes[opt]
300
4.49k
    if (Tok.isOneOf(tok::kw_asm, tok::kw___attribute))
301
8
      return TPResult::True;
302
4.49k
303
4.49k
    // initializer[opt]
304
4.49k
    if (Tok.is(tok::l_paren)) {
305
11
      // Parse through the parens.
306
11
      ConsumeParen();
307
11
      if (!SkipUntil(tok::r_paren, StopAtSemi))
308
0
        return TPResult::Error;
309
4.48k
    } else if (Tok.is(tok::l_brace)) {
310
2
      // A left-brace here is sufficient to disambiguate the parse; an
311
2
      // expression can never be followed directly by a braced-init-list.
312
2
      return TPResult::True;
313
4.47k
    } else if (Tok.is(tok::equal) || 
isTokIdentifier_in()2.93k
) {
314
1.54k
      // MSVC and g++ won't examine the rest of declarators if '=' is
315
1.54k
      // encountered; they just conclude that we have a declaration.
316
1.54k
      // EDG parses the initializer completely, which is the proper behavior
317
1.54k
      // for this case.
318
1.54k
      //
319
1.54k
      // At present, Clang follows MSVC and g++, since the parser does not have
320
1.54k
      // the ability to parse an expression fully without recording the
321
1.54k
      // results of that parse.
322
1.54k
      // FIXME: Handle this case correctly.
323
1.54k
      //
324
1.54k
      // Also allow 'in' after an Objective-C declaration as in:
325
1.54k
      // for (int (^b)(void) in array). Ideally this should be done in the
326
1.54k
      // context of parsing for-init-statement of a foreach statement only. But,
327
1.54k
      // in any other context 'in' is invalid after a declaration and parser
328
1.54k
      // issues the error regardless of outcome of this decision.
329
1.54k
      // FIXME: Change if above assumption does not hold.
330
1.54k
      return TPResult::True;
331
1.54k
    }
332
2.94k
333
2.94k
    if (!TryConsumeToken(tok::comma))
334
2.92k
      break;
335
2.94k
  }
336
11.9k
337
11.9k
  
return TPResult::Ambiguous2.92k
;
338
11.9k
}
339
340
struct Parser::ConditionDeclarationOrInitStatementState {
341
  Parser &P;
342
  bool CanBeExpression = true;
343
  bool CanBeCondition = true;
344
  bool CanBeInitStatement;
345
  bool CanBeForRangeDecl;
346
347
  ConditionDeclarationOrInitStatementState(Parser &P, bool CanBeInitStatement,
348
                                           bool CanBeForRangeDecl)
349
      : P(P), CanBeInitStatement(CanBeInitStatement),
350
584k
        CanBeForRangeDecl(CanBeForRangeDecl) {}
351
352
588k
  bool resolved() {
353
588k
    return CanBeExpression + CanBeCondition + CanBeInitStatement +
354
588k
               CanBeForRangeDecl < 2;
355
588k
  }
356
357
1.10k
  void markNotExpression() {
358
1.10k
    CanBeExpression = false;
359
1.10k
360
1.10k
    if (!resolved()) {
361
634
      // FIXME: Unify the parsing codepaths for condition variables and
362
634
      // simple-declarations so that we don't need to eagerly figure out which
363
634
      // kind we have here. (Just parse init-declarators until we reach a
364
634
      // semicolon or right paren.)
365
634
      RevertingTentativeParsingAction PA(P);
366
634
      if (CanBeForRangeDecl) {
367
122
        // Skip until we hit a ')', ';', or a ':' with no matching '?'.
368
122
        // The final case is a for range declaration, the rest are not.
369
123
        while (true) {
370
123
          unsigned QuestionColonDepth = 0;
371
123
          P.SkipUntil({tok::r_paren, tok::semi, tok::question, tok::colon},
372
123
                      StopBeforeMatch);
373
123
          if (P.Tok.is(tok::question))
374
1
            ++QuestionColonDepth;
375
122
          else if (P.Tok.is(tok::colon)) {
376
35
            if (QuestionColonDepth)
377
0
              --QuestionColonDepth;
378
35
            else {
379
35
              CanBeCondition = CanBeInitStatement = false;
380
35
              return;
381
35
            }
382
87
          } else {
383
87
            CanBeForRangeDecl = false;
384
87
            break;
385
87
          }
386
1
          P.ConsumeToken();
387
1
        }
388
512
      } else {
389
512
        // Just skip until we hit a ')' or ';'.
390
512
        P.SkipUntil(tok::r_paren, tok::semi, StopBeforeMatch);
391
512
      }
392
634
      
if (599
P.Tok.isNot(tok::r_paren)599
)
393
171
        CanBeCondition = CanBeForRangeDecl = false;
394
599
      if (P.Tok.isNot(tok::semi))
395
430
        CanBeInitStatement = false;
396
599
    }
397
1.10k
  }
398
399
22
  bool markNotCondition() {
400
22
    CanBeCondition = false;
401
22
    return resolved();
402
22
  }
403
404
22
  bool markNotForRangeDecl() {
405
22
    CanBeForRangeDecl = false;
406
22
    return resolved();
407
22
  }
408
409
586k
  bool update(TPResult IsDecl) {
410
586k
    switch (IsDecl) {
411
1.09k
    case TPResult::True:
412
1.09k
      markNotExpression();
413
1.09k
      assert(resolved() && "can't continue after tentative parsing bails out");
414
1.09k
      break;
415
583k
    case TPResult::False:
416
583k
      CanBeCondition = CanBeInitStatement = CanBeForRangeDecl = false;
417
583k
      break;
418
1.67k
    case TPResult::Ambiguous:
419
1.67k
      break;
420
0
    case TPResult::Error:
421
0
      CanBeExpression = CanBeCondition = CanBeInitStatement =
422
0
          CanBeForRangeDecl = false;
423
0
      break;
424
586k
    }
425
586k
    return resolved();
426
586k
  }
427
428
584k
  ConditionOrInitStatement result() const {
429
584k
    assert(CanBeExpression + CanBeCondition + CanBeInitStatement +
430
584k
                   CanBeForRangeDecl < 2 &&
431
584k
           "result called but not yet resolved");
432
584k
    if (CanBeExpression)
433
583k
      return ConditionOrInitStatement::Expression;
434
1.10k
    if (CanBeCondition)
435
901
      return ConditionOrInitStatement::ConditionDecl;
436
206
    if (CanBeInitStatement)
437
83
      return ConditionOrInitStatement::InitStmtDecl;
438
123
    if (CanBeForRangeDecl)
439
35
      return ConditionOrInitStatement::ForRangeDecl;
440
88
    return ConditionOrInitStatement::Error;
441
88
  }
442
};
443
444
/// Disambiguates between a declaration in a condition, a
445
/// simple-declaration in an init-statement, and an expression for
446
/// a condition of a if/switch statement.
447
///
448
///       condition:
449
///         expression
450
///         type-specifier-seq declarator '=' assignment-expression
451
/// [C++11] type-specifier-seq declarator '=' initializer-clause
452
/// [C++11] type-specifier-seq declarator braced-init-list
453
/// [GNU]   type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
454
///             '=' assignment-expression
455
///       simple-declaration:
456
///         decl-specifier-seq init-declarator-list[opt] ';'
457
///
458
/// Note that, unlike isCXXSimpleDeclaration, we must disambiguate all the way
459
/// to the ';' to disambiguate cases like 'int(x))' (an expression) from
460
/// 'int(x);' (a simple-declaration in an init-statement).
461
Parser::ConditionOrInitStatement
462
Parser::isCXXConditionDeclarationOrInitStatement(bool CanBeInitStatement,
463
584k
                                                 bool CanBeForRangeDecl) {
464
584k
  ConditionDeclarationOrInitStatementState State(*this, CanBeInitStatement,
465
584k
                                                 CanBeForRangeDecl);
466
584k
467
584k
  if (State.update(isCXXDeclarationSpecifier()))
468
583k
    return State.result();
469
818
470
818
  // It might be a declaration; we need tentative parsing.
471
818
  RevertingTentativeParsingAction PA(*this);
472
818
473
818
  // FIXME: A tag definition unambiguously tells us this is an init-statement.
474
818
  if (State.update(TryConsumeDeclarationSpecifier()))
475
0
    return State.result();
476
818
  assert(Tok.is(tok::l_paren) && "Expected '('");
477
818
478
824
  while (true) {
479
824
    // Consume a declarator.
480
824
    if (State.update(TryParseDeclarator(false/*mayBeAbstract*/)))
481
789
      return State.result();
482
35
483
35
    // Attributes, asm label, or an initializer imply this is not an expression.
484
35
    // FIXME: Disambiguate properly after an = instead of assuming that it's a
485
35
    // valid declaration.
486
35
    if (Tok.isOneOf(tok::equal, tok::kw_asm, tok::kw___attribute) ||
487
35
        
(26
getLangOpts().CPlusPlus1126
&&
Tok.is(tok::l_brace)25
)) {
488
11
      State.markNotExpression();
489
11
      return State.result();
490
11
    }
491
24
492
24
    // A colon here identifies a for-range declaration.
493
24
    if (State.CanBeForRangeDecl && 
Tok.is(tok::colon)3
)
494
2
      return ConditionOrInitStatement::ForRangeDecl;
495
22
496
22
    // At this point, it can't be a condition any more, because a condition
497
22
    // must have a brace-or-equal-initializer.
498
22
    if (State.markNotCondition())
499
0
      return State.result();
500
22
501
22
    // Likewise, it can't be a for-range declaration any more.
502
22
    if (State.markNotForRangeDecl())
503
1
      return State.result();
504
21
505
21
    // A parenthesized initializer could be part of an expression or a
506
21
    // simple-declaration.
507
21
    if (Tok.is(tok::l_paren)) {
508
1
      ConsumeParen();
509
1
      SkipUntil(tok::r_paren, StopAtSemi);
510
1
    }
511
21
512
21
    if (!TryConsumeToken(tok::comma))
513
15
      break;
514
21
  }
515
818
516
818
  // We reached the end. If it can now be some kind of decl, then it is.
517
818
  
if (15
State.CanBeCondition15
&&
Tok.is(tok::r_paren)0
)
518
0
    return ConditionOrInitStatement::ConditionDecl;
519
15
  else if (State.CanBeInitStatement && Tok.is(tok::semi))
520
2
    return ConditionOrInitStatement::InitStmtDecl;
521
13
  else
522
13
    return ConditionOrInitStatement::Expression;
523
15
}
524
525
  /// Determine whether the next set of tokens contains a type-id.
526
  ///
527
  /// The context parameter states what context we're parsing right
528
  /// now, which affects how this routine copes with the token
529
  /// following the type-id. If the context is TypeIdInParens, we have
530
  /// already parsed the '(' and we will cease lookahead when we hit
531
  /// the corresponding ')'. If the context is
532
  /// TypeIdAsTemplateArgument, we've already parsed the '<' or ','
533
  /// before this template argument, and will cease lookahead when we
534
  /// hit a '>', '>>' (in C++0x), or ','; or, in C++0x, an ellipsis immediately
535
  /// preceding such. Returns true for a type-id and false for an expression.
536
  /// If during the disambiguation process a parsing error is encountered,
537
  /// the function returns true to let the declaration parsing code handle it.
538
  ///
539
  /// type-id:
540
  ///   type-specifier-seq abstract-declarator[opt]
541
  ///
542
5.34M
bool Parser::isCXXTypeId(TentativeCXXTypeIdContext Context, bool &isAmbiguous) {
543
5.34M
544
5.34M
  isAmbiguous = false;
545
5.34M
546
5.34M
  // C++ 8.2p2:
547
5.34M
  // The ambiguity arising from the similarity between a function-style cast and
548
5.34M
  // a type-id can occur in different contexts. The ambiguity appears as a
549
5.34M
  // choice between a function-style cast expression and a declaration of a
550
5.34M
  // type. The resolution is that any construct that could possibly be a type-id
551
5.34M
  // in its syntactic context shall be considered a type-id.
552
5.34M
553
5.34M
  TPResult TPR = isCXXDeclarationSpecifier();
554
5.34M
  if (TPR != TPResult::Ambiguous)
555
5.28M
    return TPR != TPResult::False; // Returns true for TPResult::True or
556
54.7k
                                     // TPResult::Error.
557
54.7k
558
54.7k
  // FIXME: Add statistics about the number of ambiguous statements encountered
559
54.7k
  // and how they were resolved (number of declarations+number of expressions).
560
54.7k
561
54.7k
  // Ok, we have a simple-type-specifier/typename-specifier followed by a '('.
562
54.7k
  // We need tentative parsing...
563
54.7k
564
54.7k
  RevertingTentativeParsingAction PA(*this);
565
54.7k
566
54.7k
  // type-specifier-seq
567
54.7k
  TryConsumeDeclarationSpecifier();
568
54.7k
  assert(Tok.is(tok::l_paren) && "Expected '('");
569
54.7k
570
54.7k
  // declarator
571
54.7k
  TPR = TryParseDeclarator(true/*mayBeAbstract*/, false/*mayHaveIdentifier*/);
572
54.7k
573
54.7k
  // In case of an error, let the declaration parsing code handle it.
574
54.7k
  if (TPR == TPResult::Error)
575
0
    TPR = TPResult::True;
576
54.7k
577
54.7k
  if (TPR == TPResult::Ambiguous) {
578
52.4k
    // We are supposed to be inside parens, so if after the abstract declarator
579
52.4k
    // we encounter a ')' this is a type-id, otherwise it's an expression.
580
52.4k
    if (Context == TypeIdInParens && 
Tok.is(tok::r_paren)2.11k
) {
581
1.90k
      TPR = TPResult::True;
582
1.90k
      isAmbiguous = true;
583
1.90k
584
1.90k
    // We are supposed to be inside a template argument, so if after
585
1.90k
    // the abstract declarator we encounter a '>', '>>' (in C++0x), or
586
1.90k
    // ','; or, in C++0x, an ellipsis immediately preceding such, this
587
1.90k
    // is a type-id. Otherwise, it's an expression.
588
50.5k
    } else if (Context == TypeIdAsTemplateArgument &&
589
50.5k
               
(50.3k
Tok.isOneOf(tok::greater, tok::comma)50.3k
||
590
50.3k
                
(19
getLangOpts().CPlusPlus1119
&&
591
19
                 (Tok.isOneOf(tok::greatergreater,
592
19
                              tok::greatergreatergreater) ||
593
19
                  
(12
Tok.is(tok::ellipsis)12
&&
594
12
                   NextToken().isOneOf(tok::greater, tok::greatergreater,
595
4
                                       tok::greatergreatergreater,
596
50.3k
                                       tok::comma)))))) {
597
50.3k
      TPR = TPResult::True;
598
50.3k
      isAmbiguous = true;
599
50.3k
600
50.3k
    } else
601
225
      TPR = TPResult::False;
602
52.4k
  }
603
54.7k
604
54.7k
  assert(TPR == TPResult::True || TPR == TPResult::False);
605
54.7k
  return TPR == TPResult::True;
606
54.7k
}
607
608
/// Returns true if this is a C++11 attribute-specifier. Per
609
/// C++11 [dcl.attr.grammar]p6, two consecutive left square bracket tokens
610
/// always introduce an attribute. In Objective-C++11, this rule does not
611
/// apply if either '[' begins a message-send.
612
///
613
/// If Disambiguate is true, we try harder to determine whether a '[[' starts
614
/// an attribute-specifier, and return CAK_InvalidAttributeSpecifier if not.
615
///
616
/// If OuterMightBeMessageSend is true, we assume the outer '[' is either an
617
/// Obj-C message send or the start of an attribute. Otherwise, we assume it
618
/// is not an Obj-C message send.
619
///
620
/// C++11 [dcl.attr.grammar]:
621
///
622
///     attribute-specifier:
623
///         '[' '[' attribute-list ']' ']'
624
///         alignment-specifier
625
///
626
///     attribute-list:
627
///         attribute[opt]
628
///         attribute-list ',' attribute[opt]
629
///         attribute '...'
630
///         attribute-list ',' attribute '...'
631
///
632
///     attribute:
633
///         attribute-token attribute-argument-clause[opt]
634
///
635
///     attribute-token:
636
///         identifier
637
///         identifier '::' identifier
638
///
639
///     attribute-argument-clause:
640
///         '(' balanced-token-seq ')'
641
Parser::CXX11AttributeKind
642
Parser::isCXX11AttributeSpecifier(bool Disambiguate,
643
35.6M
                                  bool OuterMightBeMessageSend) {
644
35.6M
  if (Tok.is(tok::kw_alignas))
645
9.67k
    return CAK_AttributeSpecifier;
646
35.6M
647
35.6M
  if (Tok.isNot(tok::l_square) || 
NextToken().isNot(tok::l_square)138k
)
648
35.6M
    return CAK_NotAttributeSpecifier;
649
12.0k
650
12.0k
  // No tentative parsing if we don't need to look for ']]' or a lambda.
651
12.0k
  if (!Disambiguate && 
!getLangOpts().ObjC12.0k
)
652
11.6k
    return CAK_AttributeSpecifier;
653
370
654
370
  // '[[using ns: ...]]' is an attribute.
655
370
  if (GetLookAheadToken(2).is(tok::kw_using))
656
0
    return CAK_AttributeSpecifier;
657
370
658
370
  RevertingTentativeParsingAction PA(*this);
659
370
660
370
  // Opening brackets were checked for above.
661
370
  ConsumeBracket();
662
370
663
370
  if (!getLangOpts().ObjC) {
664
8
    ConsumeBracket();
665
8
666
8
    bool IsAttribute = SkipUntil(tok::r_square);
667
8
    IsAttribute &= Tok.is(tok::r_square);
668
8
669
8
    return IsAttribute ? 
CAK_AttributeSpecifier4
:
CAK_InvalidAttributeSpecifier4
;
670
8
  }
671
362
672
362
  // In Obj-C++11, we need to distinguish four situations:
673
362
  //  1a) int x[[attr]];                     C++11 attribute.
674
362
  //  1b) [[attr]];                          C++11 statement attribute.
675
362
  //   2) int x[[obj](){ return 1; }()];     Lambda in array size/index.
676
362
  //  3a) int x[[obj get]];                  Message send in array size/index.
677
362
  //  3b) [[Class alloc] init];              Message send in message send.
678
362
  //   4) [[obj]{ return self; }() doStuff]; Lambda in message send.
679
362
  // (1) is an attribute, (2) is ill-formed, and (3) and (4) are accepted.
680
362
681
362
  // Check to see if this is a lambda-expression.
682
362
  // FIXME: If this disambiguation is too slow, fold the tentative lambda parse
683
362
  // into the tentative attribute parse below.
684
362
  {
685
362
    RevertingTentativeParsingAction LambdaTPA(*this);
686
362
    LambdaIntroducer Intro;
687
362
    LambdaIntroducerTentativeParse Tentative;
688
362
    if (ParseLambdaIntroducer(Intro, &Tentative)) {
689
0
      // We hit a hard error after deciding this was not an attribute.
690
0
      // FIXME: Don't parse and annotate expressions when disambiguating
691
0
      // against an attribute.
692
0
      return CAK_NotAttributeSpecifier;
693
0
    }
694
362
695
362
    switch (Tentative) {
696
32
    case LambdaIntroducerTentativeParse::MessageSend:
697
32
      // Case 3: The inner construct is definitely a message send, so the
698
32
      // outer construct is definitely not an attribute.
699
32
      return CAK_NotAttributeSpecifier;
700
0
701
262
    case LambdaIntroducerTentativeParse::Success:
702
262
    case LambdaIntroducerTentativeParse::Incomplete:
703
262
      // This is a lambda-introducer or attribute-specifier.
704
262
      if (Tok.is(tok::r_square))
705
256
        // Case 1: C++11 attribute.
706
256
        return CAK_AttributeSpecifier;
707
6
708
6
      if (OuterMightBeMessageSend)
709
5
        // Case 4: Lambda in message send.
710
5
        return CAK_NotAttributeSpecifier;
711
1
712
1
      // Case 2: Lambda in array size / index.
713
1
      return CAK_InvalidAttributeSpecifier;
714
1
715
67
    case LambdaIntroducerTentativeParse::Invalid:
716
67
      // No idea what this is; we couldn't parse it as a lambda-introducer.
717
67
      // Might still be an attribute-specifier or a message send.
718
67
      break;
719
67
    }
720
67
  }
721
67
722
67
  ConsumeBracket();
723
67
724
67
  // If we don't have a lambda-introducer, then we have an attribute or a
725
67
  // message-send.
726
67
  bool IsAttribute = true;
727
71
  while (Tok.isNot(tok::r_square)) {
728
71
    if (Tok.is(tok::comma)) {
729
1
      // Case 1: Stray commas can only occur in attributes.
730
1
      return CAK_AttributeSpecifier;
731
1
    }
732
70
733
70
    // Parse the attribute-token, if present.
734
70
    // C++11 [dcl.attr.grammar]:
735
70
    //   If a keyword or an alternative token that satisfies the syntactic
736
70
    //   requirements of an identifier is contained in an attribute-token,
737
70
    //   it is considered an identifier.
738
70
    SourceLocation Loc;
739
70
    if (!TryParseCXX11AttributeIdentifier(Loc)) {
740
10
      IsAttribute = false;
741
10
      break;
742
10
    }
743
60
    if (Tok.is(tok::coloncolon)) {
744
48
      ConsumeToken();
745
48
      if (!TryParseCXX11AttributeIdentifier(Loc)) {
746
0
        IsAttribute = false;
747
0
        break;
748
0
      }
749
60
    }
750
60
751
60
    // Parse the attribute-argument-clause, if present.
752
60
    if (Tok.is(tok::l_paren)) {
753
10
      ConsumeParen();
754
10
      if (!SkipUntil(tok::r_paren)) {
755
0
        IsAttribute = false;
756
0
        break;
757
0
      }
758
60
    }
759
60
760
60
    TryConsumeToken(tok::ellipsis);
761
60
762
60
    if (!TryConsumeToken(tok::comma))
763
56
      break;
764
60
  }
765
67
766
67
  // An attribute must end ']]'.
767
67
  
if (66
IsAttribute66
) {
768
56
    if (Tok.is(tok::r_square)) {
769
49
      ConsumeBracket();
770
49
      IsAttribute = Tok.is(tok::r_square);
771
49
    } else {
772
7
      IsAttribute = false;
773
7
    }
774
56
  }
775
66
776
66
  if (IsAttribute)
777
49
    // Case 1: C++11 statement attribute.
778
49
    return CAK_AttributeSpecifier;
779
17
780
17
  // Case 3: Message send.
781
17
  return CAK_NotAttributeSpecifier;
782
17
}
783
784
128k
Parser::TPResult Parser::TryParsePtrOperatorSeq() {
785
189k
  while (true) {
786
189k
    if (TryAnnotateOptionalCXXScopeToken(true))
787
1
      return TPResult::Error;
788
189k
789
189k
    if (Tok.isOneOf(tok::star, tok::amp, tok::caret, tok::ampamp) ||
790
189k
        
(150k
Tok.is(tok::annot_cxxscope)150k
&&
NextToken().is(tok::star)24.9k
)) {
791
60.7k
      // ptr-operator
792
60.7k
      ConsumeAnyToken();
793
62.7k
      while (Tok.isOneOf(tok::kw_const, tok::kw_volatile, tok::kw_restrict,
794
62.7k
                         tok::kw__Nonnull, tok::kw__Nullable,
795
62.7k
                         tok::kw__Null_unspecified))
796
1.97k
        ConsumeToken();
797
128k
    } else {
798
128k
      return TPResult::True;
799
128k
    }
800
189k
  }
801
128k
}
802
803
///         operator-function-id:
804
///           'operator' operator
805
///
806
///         operator: one of
807
///           new  delete  new[]  delete[]  +  -  *  /  %  ^  [...]
808
///
809
///         conversion-function-id:
810
///           'operator' conversion-type-id
811
///
812
///         conversion-type-id:
813
///           type-specifier-seq conversion-declarator[opt]
814
///
815
///         conversion-declarator:
816
///           ptr-operator conversion-declarator[opt]
817
///
818
///         literal-operator-id:
819
///           'operator' string-literal identifier
820
///           'operator' user-defined-string-literal
821
16
Parser::TPResult Parser::TryParseOperatorId() {
822
16
  assert(Tok.is(tok::kw_operator));
823
16
  ConsumeToken();
824
16
825
16
  // Maybe this is an operator-function-id.
826
16
  switch (Tok.getKind()) {
827
0
  case tok::kw_new: case tok::kw_delete:
828
0
    ConsumeToken();
829
0
    if (Tok.is(tok::l_square) && NextToken().is(tok::r_square)) {
830
0
      ConsumeBracket();
831
0
      ConsumeBracket();
832
0
    }
833
0
    return TPResult::True;
834
0
835
0
#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemOnly) \
836
608
  case tok::Token:
837
0
#define OVERLOADED_OPERATOR_MULTI(Name, Spelling, Unary, Binary, MemOnly)
838
16
#include 
"clang/Basic/OperatorKinds.def"0
839
16
    ConsumeToken();
840
16
    return TPResult::True;
841
592
842
592
  case tok::l_square:
843
0
    if (NextToken().is(tok::r_square)) {
844
0
      ConsumeBracket();
845
0
      ConsumeBracket();
846
0
      return TPResult::True;
847
0
    }
848
0
    break;
849
0
850
0
  case tok::l_paren:
851
0
    if (NextToken().is(tok::r_paren)) {
852
0
      ConsumeParen();
853
0
      ConsumeParen();
854
0
      return TPResult::True;
855
0
    }
856
0
    break;
857
0
858
0
  default:
859
0
    break;
860
0
  }
861
0
862
0
  // Maybe this is a literal-operator-id.
863
0
  if (getLangOpts().CPlusPlus11 && isTokenStringLiteral()) {
864
0
    bool FoundUDSuffix = false;
865
0
    do {
866
0
      FoundUDSuffix |= Tok.hasUDSuffix();
867
0
      ConsumeStringToken();
868
0
    } while (isTokenStringLiteral());
869
0
870
0
    if (!FoundUDSuffix) {
871
0
      if (Tok.is(tok::identifier))
872
0
        ConsumeToken();
873
0
      else
874
0
        return TPResult::Error;
875
0
    }
876
0
    return TPResult::True;
877
0
  }
878
0
879
0
  // Maybe this is a conversion-function-id.
880
0
  bool AnyDeclSpecifiers = false;
881
0
  while (true) {
882
0
    TPResult TPR = isCXXDeclarationSpecifier();
883
0
    if (TPR == TPResult::Error)
884
0
      return TPR;
885
0
    if (TPR == TPResult::False) {
886
0
      if (!AnyDeclSpecifiers)
887
0
        return TPResult::Error;
888
0
      break;
889
0
    }
890
0
    if (TryConsumeDeclarationSpecifier() == TPResult::Error)
891
0
      return TPResult::Error;
892
0
    AnyDeclSpecifiers = true;
893
0
  }
894
0
  return TryParsePtrOperatorSeq();
895
0
}
896
897
///         declarator:
898
///           direct-declarator
899
///           ptr-operator declarator
900
///
901
///         direct-declarator:
902
///           declarator-id
903
///           direct-declarator '(' parameter-declaration-clause ')'
904
///                 cv-qualifier-seq[opt] exception-specification[opt]
905
///           direct-declarator '[' constant-expression[opt] ']'
906
///           '(' declarator ')'
907
/// [GNU]     '(' attributes declarator ')'
908
///
909
///         abstract-declarator:
910
///           ptr-operator abstract-declarator[opt]
911
///           direct-abstract-declarator
912
///
913
///         direct-abstract-declarator:
914
///           direct-abstract-declarator[opt]
915
///                 '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
916
///                 exception-specification[opt]
917
///           direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
918
///           '(' abstract-declarator ')'
919
/// [C++0x]   ...
920
///
921
///         ptr-operator:
922
///           '*' cv-qualifier-seq[opt]
923
///           '&'
924
/// [C++0x]   '&&'                                                        [TODO]
925
///           '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
926
///
927
///         cv-qualifier-seq:
928
///           cv-qualifier cv-qualifier-seq[opt]
929
///
930
///         cv-qualifier:
931
///           'const'
932
///           'volatile'
933
///
934
///         declarator-id:
935
///           '...'[opt] id-expression
936
///
937
///         id-expression:
938
///           unqualified-id
939
///           qualified-id                                                [TODO]
940
///
941
///         unqualified-id:
942
///           identifier
943
///           operator-function-id
944
///           conversion-function-id
945
///           literal-operator-id
946
///           '~' class-name                                              [TODO]
947
///           '~' decltype-specifier                                      [TODO]
948
///           template-id                                                 [TODO]
949
///
950
Parser::TPResult Parser::TryParseDeclarator(bool mayBeAbstract,
951
                                            bool mayHaveIdentifier,
952
128k
                                            bool mayHaveDirectInit) {
953
128k
  // declarator:
954
128k
  //   direct-declarator
955
128k
  //   ptr-operator declarator
956
128k
  if (TryParsePtrOperatorSeq() == TPResult::Error)
957
1
    return TPResult::Error;
958
128k
959
128k
  // direct-declarator:
960
128k
  // direct-abstract-declarator:
961
128k
  if (Tok.is(tok::ellipsis))
962
14
    ConsumeToken();
963
128k
964
128k
  if ((Tok.isOneOf(tok::identifier, tok::kw_operator) ||
965
128k
       
(116k
Tok.is(tok::annot_cxxscope)116k
&&
(2.83k
NextToken().is(tok::identifier)2.83k
||
966
2.83k
                                        
NextToken().is(tok::kw_operator)363
))) &&
967
128k
      
mayHaveIdentifier15.0k
) {
968
14.9k
    // declarator-id
969
14.9k
    if (Tok.is(tok::annot_cxxscope))
970
2.45k
      ConsumeAnnotationToken();
971
12.4k
    else if (Tok.is(tok::identifier))
972
12.4k
      TentativelyDeclaredIdentifiers.push_back(Tok.getIdentifierInfo());
973
14.9k
    if (Tok.is(tok::kw_operator)) {
974
16
      if (TryParseOperatorId() == TPResult::Error)
975
0
        return TPResult::Error;
976
14.8k
    } else
977
14.8k
      ConsumeToken();
978
113k
  } else if (Tok.is(tok::l_paren)) {
979
76.3k
    ConsumeParen();
980
76.3k
    if (mayBeAbstract &&
981
76.3k
        
(64.0k
Tok.is(tok::r_paren)64.0k
|| // 'int()' is a function.
982
64.0k
         // 'int(...)' is a function.
983
64.0k
         
(62.9k
Tok.is(tok::ellipsis)62.9k
&&
NextToken().is(tok::r_paren)1
) ||
984
64.0k
         
isDeclarationSpecifier()62.9k
)) { // 'int(int)' is a function.
985
24.6k
      // '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
986
24.6k
      //        exception-specification[opt]
987
24.6k
      TPResult TPR = TryParseFunctionDeclarator();
988
24.6k
      if (TPR != TPResult::Ambiguous)
989
1
        return TPR;
990
51.6k
    } else {
991
51.6k
      // '(' declarator ')'
992
51.6k
      // '(' attributes declarator ')'
993
51.6k
      // '(' abstract-declarator ')'
994
51.6k
      if (Tok.isOneOf(tok::kw___attribute, tok::kw___declspec, tok::kw___cdecl,
995
51.6k
                      tok::kw___stdcall, tok::kw___fastcall, tok::kw___thiscall,
996
51.6k
                      tok::kw___regcall, tok::kw___vectorcall))
997
1
        return TPResult::True; // attributes indicate declaration
998
51.6k
      TPResult TPR = TryParseDeclarator(mayBeAbstract, mayHaveIdentifier);
999
51.6k
      if (TPR != TPResult::Ambiguous)
1000
4.00k
        return TPR;
1001
47.6k
      if (Tok.isNot(tok::r_paren))
1002
6.47k
        return TPResult::False;
1003
41.1k
      ConsumeParen();
1004
41.1k
    }
1005
76.3k
  } else 
if (37.4k
!mayBeAbstract37.4k
) {
1006
4.58k
    return TPResult::False;
1007
4.58k
  }
1008
113k
1009
113k
  if (mayHaveDirectInit)
1010
44
    return TPResult::Ambiguous;
1011
113k
1012
151k
  
while (113k
1) {
1013
151k
    TPResult TPR(TPResult::Ambiguous);
1014
151k
1015
151k
    if (Tok.is(tok::l_paren)) {
1016
35.7k
      // Check whether we have a function declarator or a possible ctor-style
1017
35.7k
      // initializer that follows the declarator. Note that ctor-style
1018
35.7k
      // initializers are not possible in contexts where abstract declarators
1019
35.7k
      // are allowed.
1020
35.7k
      if (!mayBeAbstract && 
!isCXXFunctionDeclarator()3.65k
)
1021
2.42k
        break;
1022
33.3k
1023
33.3k
      // direct-declarator '(' parameter-declaration-clause ')'
1024
33.3k
      //        cv-qualifier-seq[opt] exception-specification[opt]
1025
33.3k
      ConsumeParen();
1026
33.3k
      TPR = TryParseFunctionDeclarator();
1027
116k
    } else if (Tok.is(tok::l_square)) {
1028
5.01k
      // direct-declarator '[' constant-expression[opt] ']'
1029
5.01k
      // direct-abstract-declarator[opt] '[' constant-expression[opt] ']'
1030
5.01k
      TPR = TryParseBracketDeclarator();
1031
111k
    } else if (Tok.is(tok::kw_requires)) {
1032
3
      // declarator requires-clause
1033
3
      // A requires clause indicates a function declaration.
1034
3
      TPR = TPResult::True;
1035
111k
    } else {
1036
111k
      break;
1037
111k
    }
1038
38.3k
1039
38.3k
    if (TPR != TPResult::Ambiguous)
1040
7
      return TPR;
1041
38.3k
  }
1042
113k
1043
113k
  
return TPResult::Ambiguous113k
;
1044
113k
}
1045
1046
Parser::TPResult
1047
205
Parser::isExpressionOrTypeSpecifierSimple(tok::TokenKind Kind) {
1048
205
  switch (Kind) {
1049
0
  // Obviously starts an expression.
1050
3
  case tok::numeric_constant:
1051
3
  case tok::char_constant:
1052
3
  case tok::wide_char_constant:
1053
3
  case tok::utf8_char_constant:
1054
3
  case tok::utf16_char_constant:
1055
3
  case tok::utf32_char_constant:
1056
3
  case tok::string_literal:
1057
3
  case tok::wide_string_literal:
1058
3
  case tok::utf8_string_literal:
1059
3
  case tok::utf16_string_literal:
1060
3
  case tok::utf32_string_literal:
1061
3
  case tok::l_square:
1062
3
  case tok::l_paren:
1063
3
  case tok::amp:
1064
3
  case tok::ampamp:
1065
3
  case tok::star:
1066
3
  case tok::plus:
1067
3
  case tok::plusplus:
1068
3
  case tok::minus:
1069
3
  case tok::minusminus:
1070
3
  case tok::tilde:
1071
3
  case tok::exclaim:
1072
3
  case tok::kw_sizeof:
1073
3
  case tok::kw___func__:
1074
3
  case tok::kw_const_cast:
1075
3
  case tok::kw_delete:
1076
3
  case tok::kw_dynamic_cast:
1077
3
  case tok::kw_false:
1078
3
  case tok::kw_new:
1079
3
  case tok::kw_operator:
1080
3
  case tok::kw_reinterpret_cast:
1081
3
  case tok::kw_static_cast:
1082
3
  case tok::kw_this:
1083
3
  case tok::kw_throw:
1084
3
  case tok::kw_true:
1085
3
  case tok::kw_typeid:
1086
3
  case tok::kw_alignof:
1087
3
  case tok::kw_noexcept:
1088
3
  case tok::kw_nullptr:
1089
3
  case tok::kw__Alignof:
1090
3
  case tok::kw___null:
1091
3
  case tok::kw___alignof:
1092
3
  case tok::kw___builtin_choose_expr:
1093
3
  case tok::kw___builtin_offsetof:
1094
3
  case tok::kw___builtin_va_arg:
1095
3
  case tok::kw___imag:
1096
3
  case tok::kw___real:
1097
3
  case tok::kw___FUNCTION__:
1098
3
  case tok::kw___FUNCDNAME__:
1099
3
  case tok::kw___FUNCSIG__:
1100
3
  case tok::kw_L__FUNCTION__:
1101
3
  case tok::kw_L__FUNCSIG__:
1102
3
  case tok::kw___PRETTY_FUNCTION__:
1103
3
  case tok::kw___uuidof:
1104
3
#define TYPE_TRAIT(N,Spelling,K) \
1105
192
  case tok::kw_##Spelling:
1106
3
#include "clang/Basic/TokenKinds.def"
1107
3
    return TPResult::True;
1108
189
1109
189
  // Obviously starts a type-specifier-seq:
1110
189
  case tok::kw_char:
1111
160
  case tok::kw_const:
1112
160
  case tok::kw_double:
1113
160
  case tok::kw__Float16:
1114
160
  case tok::kw___float128:
1115
160
  case tok::kw_enum:
1116
160
  case tok::kw_half:
1117
160
  case tok::kw_float:
1118
160
  case tok::kw_int:
1119
160
  case tok::kw_long:
1120
160
  case tok::kw___int64:
1121
160
  case tok::kw___int128:
1122
160
  case tok::kw_restrict:
1123
160
  case tok::kw_short:
1124
160
  case tok::kw_signed:
1125
160
  case tok::kw_struct:
1126
160
  case tok::kw_union:
1127
160
  case tok::kw_unsigned:
1128
160
  case tok::kw_void:
1129
160
  case tok::kw_volatile:
1130
160
  case tok::kw__Bool:
1131
160
  case tok::kw__Complex:
1132
160
  case tok::kw_class:
1133
160
  case tok::kw_typename:
1134
160
  case tok::kw_wchar_t:
1135
160
  case tok::kw_char8_t:
1136
160
  case tok::kw_char16_t:
1137
160
  case tok::kw_char32_t:
1138
160
  case tok::kw__Decimal32:
1139
160
  case tok::kw__Decimal64:
1140
160
  case tok::kw__Decimal128:
1141
160
  case tok::kw___interface:
1142
160
  case tok::kw___thread:
1143
160
  case tok::kw_thread_local:
1144
160
  case tok::kw__Thread_local:
1145
160
  case tok::kw_typeof:
1146
160
  case tok::kw___underlying_type:
1147
160
  case tok::kw___cdecl:
1148
160
  case tok::kw___stdcall:
1149
160
  case tok::kw___fastcall:
1150
160
  case tok::kw___thiscall:
1151
160
  case tok::kw___regcall:
1152
160
  case tok::kw___vectorcall:
1153
160
  case tok::kw___unaligned:
1154
160
  case tok::kw___vector:
1155
160
  case tok::kw___pixel:
1156
160
  case tok::kw___bool:
1157
160
  case tok::kw__Atomic:
1158
1.92k
#define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
1159
1.92k
#include 
"clang/Basic/OpenCLImageTypes.def"160
1160
1.92k
  case tok::kw___unknown_anytype:
1161
160
    return TPResult::False;
1162
1.92k
1163
1.92k
  default:
1164
42
    break;
1165
42
  }
1166
42
1167
42
  return TPResult::Ambiguous;
1168
42
}
1169
1170
6.34M
bool Parser::isTentativelyDeclared(IdentifierInfo *II) {
1171
6.34M
  return std::find(TentativelyDeclaredIdentifiers.begin(),
1172
6.34M
                   TentativelyDeclaredIdentifiers.end(), II)
1173
6.34M
      != TentativelyDeclaredIdentifiers.end();
1174
6.34M
}
1175
1176
namespace {
1177
class TentativeParseCCC final : public CorrectionCandidateCallback {
1178
public:
1179
4.39M
  TentativeParseCCC(const Token &Next) {
1180
4.39M
    WantRemainingKeywords = false;
1181
4.39M
    WantTypeSpecifiers = Next.isOneOf(tok::l_paren, tok::r_paren, tok::greater,
1182
4.39M
                                      tok::l_brace, tok::identifier);
1183
4.39M
  }
1184
1185
154
  bool ValidateCandidate(const TypoCorrection &Candidate) override {
1186
154
    // Reject any candidate that only resolves to instance members since they
1187
154
    // aren't viable as standalone identifiers instead of member references.
1188
154
    if (Candidate.isResolved() && !Candidate.isKeyword() &&
1189
154
        llvm::all_of(Candidate,
1190
134
                     [](NamedDecl *ND) { return ND->isCXXInstanceMember(); }))
1191
2
      return false;
1192
152
1193
152
    return CorrectionCandidateCallback::ValidateCandidate(Candidate);
1194
152
  }
1195
1196
180
  std::unique_ptr<CorrectionCandidateCallback> clone() override {
1197
180
    return std::make_unique<TentativeParseCCC>(*this);
1198
180
  }
1199
};
1200
}
1201
/// isCXXDeclarationSpecifier - Returns TPResult::True if it is a declaration
1202
/// specifier, TPResult::False if it is not, TPResult::Ambiguous if it could
1203
/// be either a decl-specifier or a function-style cast, and TPResult::Error
1204
/// if a parsing error was found and reported.
1205
///
1206
/// If InvalidAsDeclSpec is not null, some cases that would be ill-formed as
1207
/// declaration specifiers but possibly valid as some other kind of construct
1208
/// return TPResult::Ambiguous instead of TPResult::False. When this happens,
1209
/// the intent is to keep trying to disambiguate, on the basis that we might
1210
/// find a better reason to treat this construct as a declaration later on.
1211
/// When this happens and the name could possibly be valid in some other
1212
/// syntactic context, *InvalidAsDeclSpec is set to 'true'. The current cases
1213
/// that trigger this are:
1214
///
1215
///   * When parsing X::Y (with no 'typename') where X is dependent
1216
///   * When parsing X<Y> where X is undeclared
1217
///
1218
///         decl-specifier:
1219
///           storage-class-specifier
1220
///           type-specifier
1221
///           function-specifier
1222
///           'friend'
1223
///           'typedef'
1224
/// [C++11]   'constexpr'
1225
/// [C++20]   'consteval'
1226
/// [GNU]     attributes declaration-specifiers[opt]
1227
///
1228
///         storage-class-specifier:
1229
///           'register'
1230
///           'static'
1231
///           'extern'
1232
///           'mutable'
1233
///           'auto'
1234
/// [GNU]     '__thread'
1235
/// [C++11]   'thread_local'
1236
/// [C11]     '_Thread_local'
1237
///
1238
///         function-specifier:
1239
///           'inline'
1240
///           'virtual'
1241
///           'explicit'
1242
///
1243
///         typedef-name:
1244
///           identifier
1245
///
1246
///         type-specifier:
1247
///           simple-type-specifier
1248
///           class-specifier
1249
///           enum-specifier
1250
///           elaborated-type-specifier
1251
///           typename-specifier
1252
///           cv-qualifier
1253
///
1254
///         simple-type-specifier:
1255
///           '::'[opt] nested-name-specifier[opt] type-name
1256
///           '::'[opt] nested-name-specifier 'template'
1257
///                 simple-template-id                              [TODO]
1258
///           'char'
1259
///           'wchar_t'
1260
///           'bool'
1261
///           'short'
1262
///           'int'
1263
///           'long'
1264
///           'signed'
1265
///           'unsigned'
1266
///           'float'
1267
///           'double'
1268
///           'void'
1269
/// [GNU]     typeof-specifier
1270
/// [GNU]     '_Complex'
1271
/// [C++11]   'auto'
1272
/// [GNU]     '__auto_type'
1273
/// [C++11]   'decltype' ( expression )
1274
/// [C++1y]   'decltype' ( 'auto' )
1275
///
1276
///         type-name:
1277
///           class-name
1278
///           enum-name
1279
///           typedef-name
1280
///
1281
///         elaborated-type-specifier:
1282
///           class-key '::'[opt] nested-name-specifier[opt] identifier
1283
///           class-key '::'[opt] nested-name-specifier[opt] 'template'[opt]
1284
///               simple-template-id
1285
///           'enum' '::'[opt] nested-name-specifier[opt] identifier
1286
///
1287
///         enum-name:
1288
///           identifier
1289
///
1290
///         enum-specifier:
1291
///           'enum' identifier[opt] '{' enumerator-list[opt] '}'
1292
///           'enum' identifier[opt] '{' enumerator-list ',' '}'
1293
///
1294
///         class-specifier:
1295
///           class-head '{' member-specification[opt] '}'
1296
///
1297
///         class-head:
1298
///           class-key identifier[opt] base-clause[opt]
1299
///           class-key nested-name-specifier identifier base-clause[opt]
1300
///           class-key nested-name-specifier[opt] simple-template-id
1301
///               base-clause[opt]
1302
///
1303
///         class-key:
1304
///           'class'
1305
///           'struct'
1306
///           'union'
1307
///
1308
///         cv-qualifier:
1309
///           'const'
1310
///           'volatile'
1311
/// [GNU]     restrict
1312
///
1313
Parser::TPResult
1314
Parser::isCXXDeclarationSpecifier(Parser::TPResult BracedCastResult,
1315
14.9M
                                  bool *InvalidAsDeclSpec) {
1316
14.9M
  auto IsPlaceholderSpecifier = [&] (TemplateIdAnnotation *TemplateId,
1317
14.9M
                                     int Lookahead) {
1318
41.0k
    // We have a placeholder-constraint (we check for 'auto' or 'decltype' to
1319
41.0k
    // distinguish 'C<int>;' from 'C<int> auto c = 1;')
1320
41.0k
    return TemplateId->Kind == TNK_Concept_template &&
1321
41.0k
        GetLookAheadToken(Lookahead + 1).isOneOf(tok::kw_auto, tok::kw_decltype,
1322
49
            // If we have an identifier here, the user probably forgot the
1323
49
            // 'auto' in the placeholder constraint, e.g. 'C<int> x = 2;'
1324
49
            // This will be diagnosed nicely later, so disambiguate as a
1325
49
            // declaration.
1326
49
            tok::identifier);
1327
41.0k
  };
1328
14.9M
  switch (Tok.getKind()) {
1329
5.45M
  case tok::identifier: {
1330
5.45M
    // Check for need to substitute AltiVec __vector keyword
1331
5.45M
    // for "vector" identifier.
1332
5.45M
    if (TryAltiVecVectorToken())
1333
18.1k
      return TPResult::True;
1334
5.43M
1335
5.43M
    const Token &Next = NextToken();
1336
5.43M
    // In 'foo bar', 'foo' is always a type name outside of Objective-C.
1337
5.43M
    if (!getLangOpts().ObjC && 
Next.is(tok::identifier)5.00M
)
1338
318k
      return TPResult::True;
1339
5.11M
1340
5.11M
    if (Next.isNot(tok::coloncolon) && 
Next.isNot(tok::less)4.89M
) {
1341
4.39M
      // Determine whether this is a valid expression. If not, we will hit
1342
4.39M
      // a parse error one way or another. In that case, tell the caller that
1343
4.39M
      // this is ambiguous. Typo-correct to type and expression keywords and
1344
4.39M
      // to types and identifiers, in order to try to recover from errors.
1345
4.39M
      TentativeParseCCC CCC(Next);
1346
4.39M
      switch (TryAnnotateName(&CCC)) {
1347
0
      case ANK_Error:
1348
0
        return TPResult::Error;
1349
24
      case ANK_TentativeDecl:
1350
24
        return TPResult::False;
1351
3.79k
      case ANK_TemplateName:
1352
3.79k
        // In C++17, this could be a type template for class template argument
1353
3.79k
        // deduction. Try to form a type annotation for it. If we're in a
1354
3.79k
        // template template argument, we'll undo this when checking the
1355
3.79k
        // validity of the argument.
1356
3.79k
        if (getLangOpts().CPlusPlus17) {
1357
288
          if (TryAnnotateTypeOrScopeToken())
1358
0
            return TPResult::Error;
1359
288
          if (Tok.isNot(tok::identifier))
1360
288
            break;
1361
3.51k
        }
1362
3.51k
1363
3.51k
        // A bare type template-name which can't be a template template
1364
3.51k
        // argument is an error, and was probably intended to be a type.
1365
3.51k
        return GreaterThanIsOperator ? 
TPResult::True6
:
TPResult::False3.50k
;
1366
3.51k
      case ANK_Unresolved:
1367
749
        return InvalidAsDeclSpec ? 
TPResult::Ambiguous647
:
TPResult::False102
;
1368
4.38M
      case ANK_Success:
1369
4.38M
        break;
1370
4.38M
      }
1371
4.38M
      assert(Tok.isNot(tok::identifier) &&
1372
4.38M
             "TryAnnotateName succeeded without producing an annotation");
1373
4.38M
    } else {
1374
722k
      // This might possibly be a type with a dependent scope specifier and
1375
722k
      // a missing 'typename' keyword. Don't use TryAnnotateName in this case,
1376
722k
      // since it will annotate as a primary expression, and we want to use the
1377
722k
      // "missing 'typename'" logic.
1378
722k
      if (TryAnnotateTypeOrScopeToken())
1379
34
        return TPResult::Error;
1380
722k
      // If annotation failed, assume it's a non-type.
1381
722k
      // FIXME: If this happens due to an undeclared identifier, treat it as
1382
722k
      // ambiguous.
1383
722k
      if (Tok.is(tok::identifier))
1384
127k
        return TPResult::False;
1385
4.98M
    }
1386
4.98M
1387
4.98M
    // We annotated this token as something. Recurse to handle whatever we got.
1388
4.98M
    return isCXXDeclarationSpecifier(BracedCastResult, InvalidAsDeclSpec);
1389
4.98M
  }
1390
4.98M
1391
4.98M
  case tok::kw_typename:  // typename T::type
1392
160k
    // Annotate typenames and C++ scope specifiers.  If we get one, just
1393
160k
    // recurse to handle whatever we get.
1394
160k
    if (TryAnnotateTypeOrScopeToken())
1395
10
      return TPResult::Error;
1396
160k
    return isCXXDeclarationSpecifier(BracedCastResult, InvalidAsDeclSpec);
1397
160k
1398
160k
  case tok::coloncolon: {    // ::foo::bar
1399
14.1k
    const Token &Next = NextToken();
1400
14.1k
    if (Next.isOneOf(tok::kw_new,       // ::new
1401
14.1k
                     tok::kw_delete))   // ::delete
1402
13.8k
      return TPResult::False;
1403
325
    LLVM_FALLTHROUGH;
1404
325
  }
1405
11.0k
  case tok::kw___super:
1406
11.0k
  case tok::kw_decltype:
1407
11.0k
    // Annotate typenames and C++ scope specifiers.  If we get one, just
1408
11.0k
    // recurse to handle whatever we get.
1409
11.0k
    if (TryAnnotateTypeOrScopeToken())
1410
2
      return TPResult::Error;
1411
11.0k
    return isCXXDeclarationSpecifier(BracedCastResult, InvalidAsDeclSpec);
1412
11.0k
1413
11.0k
    // decl-specifier:
1414
11.0k
    //   storage-class-specifier
1415
11.0k
    //   type-specifier
1416
11.0k
    //   function-specifier
1417
11.0k
    //   'friend'
1418
11.0k
    //   'typedef'
1419
11.0k
    //   'constexpr'
1420
491k
  case tok::kw_friend:
1421
491k
  case tok::kw_typedef:
1422
491k
  case tok::kw_constexpr:
1423
491k
  case tok::kw_consteval:
1424
491k
  case tok::kw_constinit:
1425
491k
    // storage-class-specifier
1426
491k
  case tok::kw_register:
1427
491k
  case tok::kw_static:
1428
491k
  case tok::kw_extern:
1429
491k
  case tok::kw_mutable:
1430
491k
  case tok::kw_auto:
1431
491k
  case tok::kw___thread:
1432
491k
  case tok::kw_thread_local:
1433
491k
  case tok::kw__Thread_local:
1434
491k
    // function-specifier
1435
491k
  case tok::kw_inline:
1436
491k
  case tok::kw_virtual:
1437
491k
  case tok::kw_explicit:
1438
491k
1439
491k
    // Modules
1440
491k
  case tok::kw___module_private__:
1441
491k
1442
491k
    // Debugger support
1443
491k
  case tok::kw___unknown_anytype:
1444
491k
1445
491k
    // type-specifier:
1446
491k
    //   simple-type-specifier
1447
491k
    //   class-specifier
1448
491k
    //   enum-specifier
1449
491k
    //   elaborated-type-specifier
1450
491k
    //   typename-specifier
1451
491k
    //   cv-qualifier
1452
491k
1453
491k
    // class-specifier
1454
491k
    // elaborated-type-specifier
1455
491k
  case tok::kw_class:
1456
491k
  case tok::kw_struct:
1457
491k
  case tok::kw_union:
1458
491k
  case tok::kw___interface:
1459
491k
    // enum-specifier
1460
491k
  case tok::kw_enum:
1461
491k
    // cv-qualifier
1462
491k
  case tok::kw_const:
1463
491k
  case tok::kw_volatile:
1464
491k
    return TPResult::True;
1465
491k
1466
491k
    // OpenCL address space qualifiers
1467
491k
  case tok::kw_private:
1468
7
    if (!getLangOpts().OpenCL)
1469
1
      return TPResult::False;
1470
6
    LLVM_FALLTHROUGH;
1471
2.40k
  case tok::kw___private:
1472
2.40k
  case tok::kw___local:
1473
2.40k
  case tok::kw___global:
1474
2.40k
  case tok::kw___constant:
1475
2.40k
  case tok::kw___generic:
1476
2.40k
    // OpenCL access qualifiers
1477
2.40k
  case tok::kw___read_only:
1478
2.40k
  case tok::kw___write_only:
1479
2.40k
  case tok::kw___read_write:
1480
2.40k
    // OpenCL pipe
1481
2.40k
  case tok::kw_pipe:
1482
2.40k
1483
2.40k
    // GNU
1484
2.40k
  case tok::kw_restrict:
1485
2.40k
  case tok::kw__Complex:
1486
2.40k
  case tok::kw___attribute:
1487
2.40k
  case tok::kw___auto_type:
1488
2.40k
    return TPResult::True;
1489
2.40k
1490
2.40k
    // Microsoft
1491
2.40k
  case tok::kw___declspec:
1492
393
  case tok::kw___cdecl:
1493
393
  case tok::kw___stdcall:
1494
393
  case tok::kw___fastcall:
1495
393
  case tok::kw___thiscall:
1496
393
  case tok::kw___regcall:
1497
393
  case tok::kw___vectorcall:
1498
393
  case tok::kw___w64:
1499
393
  case tok::kw___sptr:
1500
393
  case tok::kw___uptr:
1501
393
  case tok::kw___ptr64:
1502
393
  case tok::kw___ptr32:
1503
393
  case tok::kw___forceinline:
1504
393
  case tok::kw___unaligned:
1505
393
  case tok::kw__Nonnull:
1506
393
  case tok::kw__Nullable:
1507
393
  case tok::kw__Null_unspecified:
1508
393
  case tok::kw___kindof:
1509
393
    return TPResult::True;
1510
393
1511
393
    // Borland
1512
393
  case tok::kw___pascal:
1513
4
    return TPResult::True;
1514
393
1515
393
    // AltiVec
1516
393
  case tok::kw___vector:
1517
30
    return TPResult::True;
1518
393
1519
21.6k
  case tok::annot_template_id: {
1520
21.6k
    TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
1521
21.6k
    // If lookup for the template-name found nothing, don't assume we have a
1522
21.6k
    // definitive disambiguation result yet.
1523
21.6k
    if (TemplateId->Kind == TNK_Undeclared_template && 
InvalidAsDeclSpec36
) {
1524
35
      // 'template-id(' can be a valid expression but not a valid decl spec if
1525
35
      // the template-name is not declared, but we don't consider this to be a
1526
35
      // definitive disambiguation. In any other context, it's an error either
1527
35
      // way.
1528
35
      *InvalidAsDeclSpec = NextToken().is(tok::l_paren);
1529
35
      return TPResult::Ambiguous;
1530
35
    }
1531
21.5k
    if (IsPlaceholderSpecifier(TemplateId, /*Lookahead=*/0))
1532
37
      return TPResult::True;
1533
21.5k
    if (TemplateId->Kind != TNK_Type_template)
1534
21.5k
      return TPResult::False;
1535
3
    CXXScopeSpec SS;
1536
3
    AnnotateTemplateIdTokenAsType(SS);
1537
3
    assert(Tok.is(tok::annot_typename));
1538
3
    goto case_typename;
1539
3
  }
1540
3
1541
371k
  case tok::annot_cxxscope: // foo::bar or ::foo::bar, but already parsed
1542
371k
    // We've already annotated a scope; try to annotate a type.
1543
371k
    if (TryAnnotateTypeOrScopeToken())
1544
0
      return TPResult::Error;
1545
371k
    if (!Tok.is(tok::annot_typename)) {
1546
371k
      if (Tok.is(tok::annot_cxxscope) &&
1547
371k
          NextToken().is(tok::annot_template_id)) {
1548
19.4k
        TemplateIdAnnotation *TemplateId =
1549
19.4k
            takeTemplateIdAnnotation(NextToken());
1550
19.4k
        if (IsPlaceholderSpecifier(TemplateId, /*Lookahead=*/1))
1551
4
          return TPResult::True;
1552
371k
      }
1553
371k
      // If the next token is an identifier or a type qualifier, then this
1554
371k
      // can't possibly be a valid expression either.
1555
371k
      if (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier)) {
1556
338k
        CXXScopeSpec SS;
1557
338k
        Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1558
338k
                                                     Tok.getAnnotationRange(),
1559
338k
                                                     SS);
1560
338k
        if (SS.getScopeRep() && 
SS.getScopeRep()->isDependent()338k
) {
1561
274k
          RevertingTentativeParsingAction PA(*this);
1562
274k
          ConsumeAnnotationToken();
1563
274k
          ConsumeToken();
1564
274k
          bool isIdentifier = Tok.is(tok::identifier);
1565
274k
          TPResult TPR = TPResult::False;
1566
274k
          if (!isIdentifier)
1567
274k
            TPR = isCXXDeclarationSpecifier(BracedCastResult,
1568
274k
                                            InvalidAsDeclSpec);
1569
274k
1570
274k
          if (isIdentifier ||
1571
274k
              
TPR == TPResult::True274k
||
TPR == TPResult::Error274k
)
1572
20
            return TPResult::Error;
1573
274k
1574
274k
          if (InvalidAsDeclSpec) {
1575
38.7k
            // We can't tell whether this is a missing 'typename' or a valid
1576
38.7k
            // expression.
1577
38.7k
            *InvalidAsDeclSpec = true;
1578
38.7k
            return TPResult::Ambiguous;
1579
236k
          } else {
1580
236k
            // In MS mode, if InvalidAsDeclSpec is not provided, and the tokens
1581
236k
            // are or the form *) or &) *> or &> &&>, this can't be an expression.
1582
236k
            // The typename must be missing.
1583
236k
            if (getLangOpts().MSVCCompat) {
1584
40
              if (((Tok.is(tok::amp) || 
Tok.is(tok::star)36
) &&
1585
40
                   
(6
NextToken().is(tok::r_paren)6
||
1586
6
                    
NextToken().is(tok::greater)5
)) ||
1587
40
                  
(38
Tok.is(tok::ampamp)38
&&
NextToken().is(tok::greater)0
))
1588
2
                return TPResult::True;
1589
63.4k
            }
1590
236k
          }
1591
63.4k
        } else {
1592
63.4k
          // Try to resolve the name. If it doesn't exist, assume it was
1593
63.4k
          // intended to name a type and keep disambiguating.
1594
63.4k
          switch (TryAnnotateName()) {
1595
4
          case ANK_Error:
1596
4
            return TPResult::Error;
1597
0
          case ANK_TentativeDecl:
1598
0
            return TPResult::False;
1599
47
          case ANK_TemplateName:
1600
47
            // In C++17, this could be a type template for class template
1601
47
            // argument deduction.
1602
47
            if (getLangOpts().CPlusPlus17) {
1603
0
              if (TryAnnotateTypeOrScopeToken())
1604
0
                return TPResult::Error;
1605
0
              if (Tok.isNot(tok::identifier))
1606
0
                break;
1607
47
            }
1608
47
1609
47
            // A bare type template-name which can't be a template template
1610
47
            // argument is an error, and was probably intended to be a type.
1611
47
            // In C++17, this could be class template argument deduction.
1612
47
            return (getLangOpts().CPlusPlus17 || GreaterThanIsOperator)
1613
47
                       ? 
TPResult::True9
1614
47
                       : 
TPResult::False38
;
1615
138
          case ANK_Unresolved:
1616
138
            return InvalidAsDeclSpec ? 
TPResult::Ambiguous128
:
TPResult::False10
;
1617
63.2k
          case ANK_Success:
1618
63.2k
            break;
1619
63.2k
          }
1620
63.2k
1621
63.2k
          // Annotated it, check again.
1622
63.2k
          assert(Tok.isNot(tok::annot_cxxscope) ||
1623
63.2k
                 NextToken().isNot(tok::identifier));
1624
63.2k
          return isCXXDeclarationSpecifier(BracedCastResult, InvalidAsDeclSpec);
1625
63.2k
        }
1626
338k
      }
1627
269k
      return TPResult::False;
1628
269k
    }
1629
14
    // If that succeeded, fallthrough into the generic simple-type-id case.
1630
14
    LLVM_FALLTHROUGH;
1631
14
1632
14
    // The ambiguity resides in a simple-type-specifier/typename-specifier
1633
14
    // followed by a '('. The '(' could either be the start of:
1634
14
    //
1635
14
    //   direct-declarator:
1636
14
    //     '(' declarator ')'
1637
14
    //
1638
14
    //   direct-abstract-declarator:
1639
14
    //     '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1640
14
    //              exception-specification[opt]
1641
14
    //     '(' abstract-declarator ')'
1642
14
    //
1643
14
    // or part of a function-style cast expression:
1644
14
    //
1645
14
    //     simple-type-specifier '(' expression-list[opt] ')'
1646
14
    //
1647
14
1648
14
    // simple-type-specifier:
1649
14
1650
4.27M
  case tok::annot_typename:
1651
4.27M
  case_typename:
1652
4.27M
    // In Objective-C, we might have a protocol-qualified type.
1653
4.27M
    if (getLangOpts().ObjC && 
NextToken().is(tok::less)350k
) {
1654
0
      // Tentatively parse the protocol qualifiers.
1655
0
      RevertingTentativeParsingAction PA(*this);
1656
0
      ConsumeAnyToken(); // The type token
1657
0
1658
0
      TPResult TPR = TryParseProtocolQualifiers();
1659
0
      bool isFollowedByParen = Tok.is(tok::l_paren);
1660
0
      bool isFollowedByBrace = Tok.is(tok::l_brace);
1661
0
1662
0
      if (TPR == TPResult::Error)
1663
0
        return TPResult::Error;
1664
0
1665
0
      if (isFollowedByParen)
1666
0
        return TPResult::Ambiguous;
1667
0
1668
0
      if (getLangOpts().CPlusPlus11 && isFollowedByBrace)
1669
0
        return BracedCastResult;
1670
0
1671
0
      return TPResult::True;
1672
0
    }
1673
4.27M
    LLVM_FALLTHROUGH;
1674
4.27M
1675
5.40M
  case tok::kw_char:
1676
5.40M
  case tok::kw_wchar_t:
1677
5.40M
  case tok::kw_char8_t:
1678
5.40M
  case tok::kw_char16_t:
1679
5.40M
  case tok::kw_char32_t:
1680
5.40M
  case tok::kw_bool:
1681
5.40M
  case tok::kw_short:
1682
5.40M
  case tok::kw_int:
1683
5.40M
  case tok::kw_long:
1684
5.40M
  case tok::kw___int64:
1685
5.40M
  case tok::kw___int128:
1686
5.40M
  case tok::kw_signed:
1687
5.40M
  case tok::kw_unsigned:
1688
5.40M
  case tok::kw_half:
1689
5.40M
  case tok::kw_float:
1690
5.40M
  case tok::kw_double:
1691
5.40M
  case tok::kw__Float16:
1692
5.40M
  case tok::kw___float128:
1693
5.40M
  case tok::kw_void:
1694
5.40M
  case tok::annot_decltype:
1695
64.9M
#define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
1696
64.9M
#include 
"clang/Basic/OpenCLImageTypes.def"5.40M
1697
64.9M
    if (
NextToken().is(tok::l_paren)5.40M
)
1698
77.9k
      return TPResult::Ambiguous;
1699
5.33M
1700
5.33M
    // This is a function-style cast in all cases we disambiguate other than
1701
5.33M
    // one:
1702
5.33M
    //   struct S {
1703
5.33M
    //     enum E : int { a = 4 }; // enum
1704
5.33M
    //     enum E : int { 4 };     // bit-field
1705
5.33M
    //   };
1706
5.33M
    if (getLangOpts().CPlusPlus11 && 
NextToken().is(tok::l_brace)5.29M
)
1707
791
      return BracedCastResult;
1708
5.33M
1709
5.33M
    if (isStartOfObjCClassMessageMissingOpenBracket())
1710
3
      return TPResult::False;
1711
5.33M
1712
5.33M
    return TPResult::True;
1713
5.33M
1714
5.33M
  // GNU typeof support.
1715
5.33M
  case tok::kw_typeof: {
1716
687
    if (NextToken().isNot(tok::l_paren))
1717
3
      return TPResult::True;
1718
684
1719
684
    RevertingTentativeParsingAction PA(*this);
1720
684
1721
684
    TPResult TPR = TryParseTypeofSpecifier();
1722
684
    bool isFollowedByParen = Tok.is(tok::l_paren);
1723
684
    bool isFollowedByBrace = Tok.is(tok::l_brace);
1724
684
1725
684
    if (TPR == TPResult::Error)
1726
0
      return TPResult::Error;
1727
684
1728
684
    if (isFollowedByParen)
1729
14
      return TPResult::Ambiguous;
1730
670
1731
670
    if (getLangOpts().CPlusPlus11 && 
isFollowedByBrace634
)
1732
0
      return BracedCastResult;
1733
670
1734
670
    return TPResult::True;
1735
670
  }
1736
670
1737
670
  // C++0x type traits support
1738
670
  case tok::kw___underlying_type:
1739
7
    return TPResult::True;
1740
670
1741
670
  // C11 _Atomic
1742
670
  case tok::kw__Atomic:
1743
22
    return TPResult::True;
1744
670
1745
3.01M
  default:
1746
3.01M
    return TPResult::False;
1747
14.9M
  }
1748
14.9M
}
1749
1750
9.38k
bool Parser::isCXXDeclarationSpecifierAType() {
1751
9.38k
  switch (Tok.getKind()) {
1752
0
    // typename-specifier
1753
5.80k
  case tok::annot_decltype:
1754
5.80k
  case tok::annot_template_id:
1755
5.80k
  case tok::annot_typename:
1756
5.80k
  case tok::kw_typeof:
1757
5.80k
  case tok::kw___underlying_type:
1758
5.80k
    return true;
1759
5.80k
1760
5.80k
    // elaborated-type-specifier
1761
5.80k
  case tok::kw_class:
1762
5
  case tok::kw_struct:
1763
5
  case tok::kw_union:
1764
5
  case tok::kw___interface:
1765
5
  case tok::kw_enum:
1766
5
    return true;
1767
5
1768
5
    // simple-type-specifier
1769
3.05k
  case tok::kw_char:
1770
3.05k
  case tok::kw_wchar_t:
1771
3.05k
  case tok::kw_char8_t:
1772
3.05k
  case tok::kw_char16_t:
1773
3.05k
  case tok::kw_char32_t:
1774
3.05k
  case tok::kw_bool:
1775
3.05k
  case tok::kw_short:
1776
3.05k
  case tok::kw_int:
1777
3.05k
  case tok::kw_long:
1778
3.05k
  case tok::kw___int64:
1779
3.05k
  case tok::kw___int128:
1780
3.05k
  case tok::kw_signed:
1781
3.05k
  case tok::kw_unsigned:
1782
3.05k
  case tok::kw_half:
1783
3.05k
  case tok::kw_float:
1784
3.05k
  case tok::kw_double:
1785
3.05k
  case tok::kw__Float16:
1786
3.05k
  case tok::kw___float128:
1787
3.05k
  case tok::kw_void:
1788
3.05k
  case tok::kw___unknown_anytype:
1789
3.05k
  case tok::kw___auto_type:
1790
36.6k
#define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
1791
3.05k
#include "clang/Basic/OpenCLImageTypes.def"
1792
3.05k
    return true;
1793
33.6k
1794
33.6k
  case tok::kw_auto:
1795
0
    return getLangOpts().CPlusPlus11;
1796
33.6k
1797
33.6k
  case tok::kw__Atomic:
1798
0
    // "_Atomic foo"
1799
0
    return NextToken().is(tok::l_paren);
1800
33.6k
1801
33.6k
  default:
1802
527
    return false;
1803
9.38k
  }
1804
9.38k
}
1805
1806
/// [GNU] typeof-specifier:
1807
///         'typeof' '(' expressions ')'
1808
///         'typeof' '(' type-name ')'
1809
///
1810
684
Parser::TPResult Parser::TryParseTypeofSpecifier() {
1811
684
  assert(Tok.is(tok::kw_typeof) && "Expected 'typeof'!");
1812
684
  ConsumeToken();
1813
684
1814
684
  assert(Tok.is(tok::l_paren) && "Expected '('");
1815
684
  // Parse through the parens after 'typeof'.
1816
684
  ConsumeParen();
1817
684
  if (!SkipUntil(tok::r_paren, StopAtSemi))
1818
0
    return TPResult::Error;
1819
684
1820
684
  return TPResult::Ambiguous;
1821
684
}
1822
1823
/// [ObjC] protocol-qualifiers:
1824
////         '<' identifier-list '>'
1825
0
Parser::TPResult Parser::TryParseProtocolQualifiers() {
1826
0
  assert(Tok.is(tok::less) && "Expected '<' for qualifier list");
1827
0
  ConsumeToken();
1828
0
  do {
1829
0
    if (Tok.isNot(tok::identifier))
1830
0
      return TPResult::Error;
1831
0
    ConsumeToken();
1832
0
1833
0
    if (Tok.is(tok::comma)) {
1834
0
      ConsumeToken();
1835
0
      continue;
1836
0
    }
1837
0
1838
0
    if (Tok.is(tok::greater)) {
1839
0
      ConsumeToken();
1840
0
      return TPResult::Ambiguous;
1841
0
    }
1842
0
  } while (false);
1843
0
1844
0
  return TPResult::Error;
1845
0
}
1846
1847
/// isCXXFunctionDeclarator - Disambiguates between a function declarator or
1848
/// a constructor-style initializer, when parsing declaration statements.
1849
/// Returns true for function declarator and false for constructor-style
1850
/// initializer.
1851
/// If during the disambiguation process a parsing error is encountered,
1852
/// the function returns true to let the declaration parsing code handle it.
1853
///
1854
/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
1855
///         exception-specification[opt]
1856
///
1857
1.55M
bool Parser::isCXXFunctionDeclarator(bool *IsAmbiguous) {
1858
1.55M
1859
1.55M
  // C++ 8.2p1:
1860
1.55M
  // The ambiguity arising from the similarity between a function-style cast and
1861
1.55M
  // a declaration mentioned in 6.8 can also occur in the context of a
1862
1.55M
  // declaration. In that context, the choice is between a function declaration
1863
1.55M
  // with a redundant set of parentheses around a parameter name and an object
1864
1.55M
  // declaration with a function-style cast as the initializer. Just as for the
1865
1.55M
  // ambiguities mentioned in 6.8, the resolution is to consider any construct
1866
1.55M
  // that could possibly be a declaration a declaration.
1867
1.55M
1868
1.55M
  RevertingTentativeParsingAction PA(*this);
1869
1.55M
1870
1.55M
  ConsumeParen();
1871
1.55M
  bool InvalidAsDeclaration = false;
1872
1.55M
  TPResult TPR = TryParseParameterDeclarationClause(&InvalidAsDeclaration);
1873
1.55M
  if (TPR == TPResult::Ambiguous) {
1874
90.6k
    if (Tok.isNot(tok::r_paren))
1875
8
      TPR = TPResult::False;
1876
90.6k
    else {
1877
90.6k
      const Token &Next = NextToken();
1878
90.6k
      if (Next.isOneOf(tok::amp, tok::ampamp, tok::kw_const, tok::kw_volatile,
1879
90.6k
                       tok::kw_throw, tok::kw_noexcept, tok::l_square,
1880
90.6k
                       tok::l_brace, tok::kw_try, tok::equal, tok::arrow) ||
1881
90.6k
          
isCXX11VirtSpecifier(Next)13.9k
)
1882
76.7k
        // The next token cannot appear after a constructor-style initializer,
1883
76.7k
        // and can appear next in a function definition. This must be a function
1884
76.7k
        // declarator.
1885
76.7k
        TPR = TPResult::True;
1886
13.9k
      else if (InvalidAsDeclaration)
1887
72
        // Use the absence of 'typename' as a tie-breaker.
1888
72
        TPR = TPResult::False;
1889
90.6k
    }
1890
90.6k
  }
1891
1.55M
1892
1.55M
  if (IsAmbiguous && 
TPR == TPResult::Ambiguous1.54M
)
1893
13.7k
    *IsAmbiguous = true;
1894
1.55M
1895
1.55M
  // In case of an error, let the declaration parsing code handle it.
1896
1.55M
  return TPR != TPResult::False;
1897
1.55M
}
1898
1899
/// parameter-declaration-clause:
1900
///   parameter-declaration-list[opt] '...'[opt]
1901
///   parameter-declaration-list ',' '...'
1902
///
1903
/// parameter-declaration-list:
1904
///   parameter-declaration
1905
///   parameter-declaration-list ',' parameter-declaration
1906
///
1907
/// parameter-declaration:
1908
///   attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
1909
///   attribute-specifier-seq[opt] decl-specifier-seq declarator attributes[opt]
1910
///     '=' assignment-expression
1911
///   attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
1912
///     attributes[opt]
1913
///   attribute-specifier-seq[opt] decl-specifier-seq abstract-declarator[opt]
1914
///     attributes[opt] '=' assignment-expression
1915
///
1916
Parser::TPResult
1917
Parser::TryParseParameterDeclarationClause(bool *InvalidAsDeclaration,
1918
1.60M
                                           bool VersusTemplateArgument) {
1919
1.60M
1920
1.60M
  if (Tok.is(tok::r_paren))
1921
89.6k
    return TPResult::Ambiguous;
1922
1.51M
1923
1.51M
  //   parameter-declaration-list[opt] '...'[opt]
1924
1.51M
  //   parameter-declaration-list ',' '...'
1925
1.51M
  //
1926
1.51M
  // parameter-declaration-list:
1927
1.51M
  //   parameter-declaration
1928
1.51M
  //   parameter-declaration-list ',' parameter-declaration
1929
1.51M
  //
1930
1.52M
  
while (1.51M
1) {
1931
1.52M
    // '...'[opt]
1932
1.52M
    if (Tok.is(tok::ellipsis)) {
1933
1.46k
      ConsumeToken();
1934
1.46k
      if (Tok.is(tok::r_paren))
1935
1.46k
        return TPResult::True; // '...)' is a sign of a function declarator.
1936
0
      else
1937
0
        return TPResult::False;
1938
1.51M
    }
1939
1.51M
1940
1.51M
    // An attribute-specifier-seq here is a sign of a function declarator.
1941
1.51M
    if (isCXX11AttributeSpecifier(/*Disambiguate*/false,
1942
1.51M
                                  /*OuterMightBeMessageSend*/true))
1943
8
      return TPResult::True;
1944
1.51M
1945
1.51M
    ParsedAttributes attrs(AttrFactory);
1946
1.51M
    MaybeParseMicrosoftAttributes(attrs);
1947
1.51M
1948
1.51M
    // decl-specifier-seq
1949
1.51M
    // A parameter-declaration's initializer must be preceded by an '=', so
1950
1.51M
    // decl-specifier-seq '{' is not a parameter in C++11.
1951
1.51M
    TPResult TPR = isCXXDeclarationSpecifier(TPResult::False,
1952
1.51M
                                             InvalidAsDeclaration);
1953
1.51M
    // A declaration-specifier (not followed by '(' or '{') means this can't be
1954
1.51M
    // an expression, but it could still be a template argument.
1955
1.51M
    if (TPR != TPResult::Ambiguous &&
1956
1.51M
        
!(1.50M
VersusTemplateArgument1.50M
&&
TPR == TPResult::True41
))
1957
1.50M
      return TPR;
1958
9.38k
1959
9.38k
    bool SeenType = false;
1960
9.38k
    do {
1961
9.38k
      SeenType |= isCXXDeclarationSpecifierAType();
1962
9.38k
      if (TryConsumeDeclarationSpecifier() == TPResult::Error)
1963
2
        return TPResult::Error;
1964
9.38k
1965
9.38k
      // If we see a parameter name, this can't be a template argument.
1966
9.38k
      if (SeenType && 
Tok.is(tok::identifier)8.86k
)
1967
11
        return TPResult::True;
1968
9.37k
1969
9.37k
      TPR = isCXXDeclarationSpecifier(TPResult::False,
1970
9.37k
                                      InvalidAsDeclaration);
1971
9.37k
      if (TPR == TPResult::Error)
1972
0
        return TPR;
1973
9.37k
1974
9.37k
      // Two declaration-specifiers means this can't be an expression.
1975
9.37k
      if (TPR == TPResult::True && 
!VersusTemplateArgument1
)
1976
1
        return TPR;
1977
9.37k
    } while (TPR != TPResult::False);
1978
9.38k
1979
9.38k
    // declarator
1980
9.38k
    // abstract-declarator[opt]
1981
9.38k
    TPR = TryParseDeclarator(true/*mayBeAbstract*/);
1982
9.37k
    if (TPR != TPResult::Ambiguous)
1983
448
      return TPR;
1984
8.92k
1985
8.92k
    // [GNU] attributes[opt]
1986
8.92k
    if (Tok.is(tok::kw___attribute))
1987
1
      return TPResult::True;
1988
8.92k
1989
8.92k
    // If we're disambiguating a template argument in a default argument in
1990
8.92k
    // a class definition versus a parameter declaration, an '=' here
1991
8.92k
    // disambiguates the parse one way or the other.
1992
8.92k
    // If this is a parameter, it must have a default argument because
1993
8.92k
    //   (a) the previous parameter did, and
1994
8.92k
    //   (b) this must be the first declaration of the function, so we can't
1995
8.92k
    //       inherit any default arguments from elsewhere.
1996
8.92k
    // If we see an ')', then we've reached the end of a
1997
8.92k
    // parameter-declaration-clause, and the last param is missing its default
1998
8.92k
    // argument.
1999
8.92k
    if (VersusTemplateArgument)
2000
25
      return Tok.isOneOf(tok::equal, tok::r_paren) ? 
TPResult::True4
2001
25
                                                   : 
TPResult::False21
;
2002
8.89k
2003
8.89k
    if (Tok.is(tok::equal)) {
2004
9
      // '=' assignment-expression
2005
9
      // Parse through assignment-expression.
2006
9
      // FIXME: assignment-expression may contain an unparenthesized comma.
2007
9
      if (!SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch))
2008
0
        return TPResult::Error;
2009
8.89k
    }
2010
8.89k
2011
8.89k
    if (Tok.is(tok::ellipsis)) {
2012
2
      ConsumeToken();
2013
2
      if (Tok.is(tok::r_paren))
2014
1
        return TPResult::True; // '...)' is a sign of a function declarator.
2015
1
      else
2016
1
        return TPResult::False;
2017
8.89k
    }
2018
8.89k
2019
8.89k
    if (!TryConsumeToken(tok::comma))
2020
7.54k
      break;
2021
8.89k
  }
2022
1.51M
2023
1.51M
  
return TPResult::Ambiguous7.54k
;
2024
1.51M
}
2025
2026
/// TryParseFunctionDeclarator - We parsed a '(' and we want to try to continue
2027
/// parsing as a function declarator.
2028
/// If TryParseFunctionDeclarator fully parsed the function declarator, it will
2029
/// return TPResult::Ambiguous, otherwise it will return either False() or
2030
/// Error().
2031
///
2032
/// '(' parameter-declaration-clause ')' cv-qualifier-seq[opt]
2033
///         exception-specification[opt]
2034
///
2035
/// exception-specification:
2036
///   'throw' '(' type-id-list[opt] ')'
2037
///
2038
58.0k
Parser::TPResult Parser::TryParseFunctionDeclarator() {
2039
58.0k
  // The '(' is already parsed.
2040
58.0k
2041
58.0k
  TPResult TPR = TryParseParameterDeclarationClause();
2042
58.0k
  if (TPR == TPResult::Ambiguous && 
Tok.isNot(tok::r_paren)6.51k
)
2043
0
    TPR = TPResult::False;
2044
58.0k
2045
58.0k
  if (TPR == TPResult::False || 
TPR == TPResult::Error58.0k
)
2046
1
    return TPR;
2047
58.0k
2048
58.0k
  // Parse through the parens.
2049
58.0k
  if (!SkipUntil(tok::r_paren, StopAtSemi))
2050
1
    return TPResult::Error;
2051
58.0k
2052
58.0k
  // cv-qualifier-seq
2053
78.2k
  
while (58.0k
Tok.isOneOf(tok::kw_const, tok::kw_volatile, tok::kw___unaligned,
2054
78.2k
                     tok::kw_restrict))
2055
20.1k
    ConsumeToken();
2056
58.0k
2057
58.0k
  // ref-qualifier[opt]
2058
58.0k
  if (Tok.isOneOf(tok::amp, tok::ampamp))
2059
9.48k
    ConsumeToken();
2060
58.0k
2061
58.0k
  // exception-specification
2062
58.0k
  if (Tok.is(tok::kw_throw)) {
2063
75
    ConsumeToken();
2064
75
    if (Tok.isNot(tok::l_paren))
2065
0
      return TPResult::Error;
2066
75
2067
75
    // Parse through the parens after 'throw'.
2068
75
    ConsumeParen();
2069
75
    if (!SkipUntil(tok::r_paren, StopAtSemi))
2070
0
      return TPResult::Error;
2071
58.0k
  }
2072
58.0k
  if (Tok.is(tok::kw_noexcept)) {
2073
119
    ConsumeToken();
2074
119
    // Possibly an expression as well.
2075
119
    if (Tok.is(tok::l_paren)) {
2076
27
      // Find the matching rparen.
2077
27
      ConsumeParen();
2078
27
      if (!SkipUntil(tok::r_paren, StopAtSemi))
2079
0
        return TPResult::Error;
2080
58.0k
    }
2081
119
  }
2082
58.0k
2083
58.0k
  return TPResult::Ambiguous;
2084
58.0k
}
2085
2086
/// '[' constant-expression[opt] ']'
2087
///
2088
5.01k
Parser::TPResult Parser::TryParseBracketDeclarator() {
2089
5.01k
  ConsumeBracket();
2090
5.01k
2091
5.01k
  // A constant-expression cannot begin with a '{', but the
2092
5.01k
  // expr-or-braced-init-list of a postfix-expression can.
2093
5.01k
  if (Tok.is(tok::l_brace))
2094
2
    return TPResult::False;
2095
5.00k
2096
5.00k
  if (!SkipUntil(tok::r_square, tok::comma, StopAtSemi | StopBeforeMatch))
2097
0
    return TPResult::Error;
2098
5.00k
2099
5.00k
  // If we hit a comma before the ']', this is not a constant-expression,
2100
5.00k
  // but might still be the expr-or-braced-init-list of a postfix-expression.
2101
5.00k
  if (Tok.isNot(tok::r_square))
2102
1
    return TPResult::False;
2103
5.00k
2104
5.00k
  ConsumeBracket();
2105
5.00k
  return TPResult::Ambiguous;
2106
5.00k
}
2107
2108
/// Determine whether we might be looking at the '<' template-argument-list '>'
2109
/// of a template-id or simple-template-id, rather than a less-than comparison.
2110
/// This will often fail and produce an ambiguity, but should never be wrong
2111
/// if it returns True or False.
2112
4.84k
Parser::TPResult Parser::isTemplateArgumentList(unsigned TokensToSkip) {
2113
4.84k
  if (!TokensToSkip) {
2114
2.28k
    if (Tok.isNot(tok::less))
2115
0
      return TPResult::False;
2116
2.28k
    if (NextToken().is(tok::greater))
2117
0
      return TPResult::True;
2118
4.84k
  }
2119
4.84k
2120
4.84k
  RevertingTentativeParsingAction PA(*this);
2121
4.84k
2122
7.40k
  while (TokensToSkip) {
2123
2.56k
    ConsumeAnyToken();
2124
2.56k
    --TokensToSkip;
2125
2.56k
  }
2126
4.84k
2127
4.84k
  if (!TryConsumeToken(tok::less))
2128
0
    return TPResult::False;
2129
4.84k
2130
4.84k
  // We can't do much to tell an expression apart from a template-argument,
2131
4.84k
  // but one good distinguishing factor is that a "decl-specifier" not
2132
4.84k
  // followed by '(' or '{' can't appear in an expression.
2133
4.84k
  bool InvalidAsTemplateArgumentList = false;
2134
4.84k
  if (isCXXDeclarationSpecifier(TPResult::False,
2135
4.84k
                                       &InvalidAsTemplateArgumentList) ==
2136
4.84k
             TPResult::True)
2137
208
    return TPResult::True;
2138
4.63k
  if (InvalidAsTemplateArgumentList)
2139
2
    return TPResult::False;
2140
4.63k
2141
4.63k
  // FIXME: In many contexts, X<thing1, Type> can only be a
2142
4.63k
  // template-argument-list. But that's not true in general:
2143
4.63k
  //
2144
4.63k
  // using b = int;
2145
4.63k
  // void f() {
2146
4.63k
  //   int a = A<B, b, c = C>D; // OK, declares b, not a template-id.
2147
4.63k
  //
2148
4.63k
  // X<Y<0, int> // ', int>' might be end of X's template argument list
2149
4.63k
  //
2150
4.63k
  // We might be able to disambiguate a few more cases if we're careful.
2151
4.63k
2152
4.63k
  // A template-argument-list must be terminated by a '>'.
2153
4.63k
  if (SkipUntil({tok::greater, tok::greatergreater, tok::greatergreatergreater},
2154
4.63k
                StopAtSemi | StopBeforeMatch))
2155
108
    return TPResult::Ambiguous;
2156
4.52k
  return TPResult::False;
2157
4.52k
}
2158
2159
/// Determine whether we might be looking at the '(' of a C++20 explicit(bool)
2160
/// in an earlier language mode.
2161
37
Parser::TPResult Parser::isExplicitBool() {
2162
37
  assert(Tok.is(tok::l_paren) && "expected to be looking at a '(' token");
2163
37
2164
37
  RevertingTentativeParsingAction PA(*this);
2165
37
  ConsumeParen();
2166
37
2167
37
  // We can only have 'explicit' on a constructor, conversion function, or
2168
37
  // deduction guide. The declarator of a deduction guide cannot be
2169
37
  // parenthesized, so we know this isn't a deduction guide. So the only
2170
37
  // thing we need to check for is some number of parens followed by either
2171
37
  // the current class name or 'operator'.
2172
39
  while (Tok.is(tok::l_paren))
2173
2
    ConsumeParen();
2174
37
2175
37
  if (TryAnnotateOptionalCXXScopeToken())
2176
0
    return TPResult::Error;
2177
37
2178
37
  // Class-scope constructor and conversion function names can't really be
2179
37
  // qualified, but we get better diagnostics if we assume they can be.
2180
37
  CXXScopeSpec SS;
2181
37
  if (Tok.is(tok::annot_cxxscope)) {
2182
6
    Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2183
6
                                                 Tok.getAnnotationRange(),
2184
6
                                                 SS);
2185
6
    ConsumeAnnotationToken();
2186
6
  }
2187
37
2188
37
  // 'explicit(operator' might be explicit(bool) or the declaration of a
2189
37
  // conversion function, but it's probably a conversion function.
2190
37
  if (Tok.is(tok::kw_operator))
2191
3
    return TPResult::Ambiguous;
2192
34
2193
34
  // If this can't be a constructor name, it can only be explicit(bool).
2194
34
  if (Tok.isNot(tok::identifier) && 
Tok.isNot(tok::annot_template_id)9
)
2195
9
    return TPResult::True;
2196
25
  if (!Actions.isCurrentClassName(Tok.is(tok::identifier)
2197
25
                                      ? *Tok.getIdentifierInfo()
2198
25
                                      : 
*takeTemplateIdAnnotation(Tok)->Name0
,
2199
25
                                  getCurScope(), &SS))
2200
22
    return TPResult::True;
2201
3
  // Formally, we must have a right-paren after the constructor name to match
2202
3
  // the grammar for a constructor. But clang permits a parenthesized
2203
3
  // constructor declarator, so also allow a constructor declarator to follow
2204
3
  // with no ')' token after the constructor name.
2205
3
  if (!NextToken().is(tok::r_paren) &&
2206
3
      !isConstructorDeclarator(/*Unqualified=*/SS.isEmpty(),
2207
1
                               /*DeductionGuide=*/false))
2208
0
    return TPResult::True;
2209
3
2210
3
  // Might be explicit(bool) or a parenthesized constructor name.
2211
3
  return TPResult::Ambiguous;
2212
3
}