Coverage Report

Created: 2021-01-23 06:44

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