File: ParsePattern.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (1572 lines) | stat: -rw-r--r-- 58,228 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
//===--- ParsePattern.cpp - Swift Language Parser for Patterns ------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// Pattern Parsing and AST Building
//
//===----------------------------------------------------------------------===//

#include "swift/Parse/Parser.h"

#include "swift/AST/ASTWalker.h"
#include "swift/AST/GenericParamList.h"
#include "swift/AST/Initializer.h"
#include "swift/AST/Module.h"
#include "swift/AST/SourceFile.h"
#include "swift/AST/TypeRepr.h"
#include "swift/Basic/StringExtras.h"
#include "swift/Parse/IDEInspectionCallbacks.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/Support/SaveAndRestore.h"

using namespace swift;

void Parser::DefaultArgumentInfo::setFunctionContext(
    DeclContext *DC, ParameterList *paramList){
  for (auto context : ParsedContexts) {
    context->changeFunction(DC, paramList);
  }
}

static ParserStatus parseDefaultArgument(
    Parser &P, Parser::DefaultArgumentInfo *defaultArgs, unsigned argIndex,
    Expr *&init, bool &hasInheritedDefaultArg,
    Parser::ParameterContextKind paramContext) {
  assert(P.Tok.is(tok::equal) ||
       (P.Tok.isBinaryOperator() && P.Tok.getText() == "=="));
  SourceLoc equalLoc = P.consumeToken();

  if (P.SF.Kind == SourceFileKind::Interface) {
    // Swift module interfaces don't synthesize inherited initializers and
    // instead include them explicitly in subclasses. Since the
    // \c DefaultArgumentKind of these initializers is \c Inherited, this is
    // represented textually as `= super` in the interface.

    // If we're in a module interface and the default argument is exactly
    // `super` (i.e. the token after that is `,` or `)` which end a parameter)
    // report an inherited default argument to the caller and return.
    if (P.Tok.is(tok::kw_super) && P.peekToken().isAny(tok::comma, tok::r_paren)) {
      hasInheritedDefaultArg = true;
      P.consumeToken(tok::kw_super);
      defaultArgs->HasDefaultArgument = true;
      return ParserStatus();
    }
  }

  // Enter a fresh default-argument context with a meaningless parent.
  // We'll change the parent to the function later after we've created
  // that declaration.
  auto initDC = new (P.Context) DefaultArgumentInitializer(P.CurDeclContext,
                                                           argIndex);
  Parser::ParseFunctionBody initScope(P, initDC);

  ParserResult<Expr> initR = P.parseExpr(diag::expected_init_value);

  // Record the default-argument context if we're supposed to accept default
  // arguments here.
  if (defaultArgs) {
    defaultArgs->ParsedContexts.push_back(initDC);
  }

  Diag<> diagID = { DiagID() };
  switch (paramContext) {
  case Parser::ParameterContextKind::Function:
  case Parser::ParameterContextKind::Operator:
  case Parser::ParameterContextKind::Initializer:
  case Parser::ParameterContextKind::EnumElement:
  case Parser::ParameterContextKind::Subscript:
  case Parser::ParameterContextKind::Macro:
    break;
  case Parser::ParameterContextKind::Closure:
    diagID = diag::no_default_arg_closure;
    break;
  case Parser::ParameterContextKind::Curried:
    diagID = diag::no_default_arg_curried;
    break;
  }
  
  assert((diagID.ID != DiagID()) == !defaultArgs &&
         "Default arguments specified for an unexpected parameter list kind");
  
  if (diagID.ID != DiagID()) {
    auto inFlight = P.diagnose(equalLoc, diagID);
    if (initR.isNonNull())
      inFlight.fixItRemove(SourceRange(equalLoc, initR.get()->getEndLoc()));
    return ParserStatus();
  }
  
  defaultArgs->HasDefaultArgument = true;

  if (initR.hasCodeCompletion()) {
    init = initR.get();
    return makeParserCodeCompletionStatus();
  }

  if (initR.isNull())
    return makeParserError();

  init = initR.get();
  return ParserStatus();
}

/// Determine whether we are at the start of a parameter name when
/// parsing a parameter.
bool Parser::startsParameterName(bool isClosure) {
  // To have a parameter name here, we need a name.
  if (!Tok.canBeArgumentLabel())
    return false;

  // If the next token is ':', this is a name.
  const auto &nextTok = peekToken();
  if (nextTok.is(tok::colon))
    return true;



  // If the next token can be an argument label, we might have a name.
  if (nextTok.canBeArgumentLabel()) {
    // If the first name wasn't a contextual keyword, we're done.
    if (!Tok.isContextualKeyword("isolated") &&
        !Tok.isContextualKeyword("some") && !Tok.isContextualKeyword("any") &&
        !Tok.isContextualKeyword("each") &&
        !Tok.isContextualKeyword("__shared") &&
        !Tok.isContextualKeyword("__owned") &&
        !Tok.isContextualKeyword("borrowing") &&
        (!Context.LangOpts.hasFeature(Feature::SendingArgsAndResults) ||
         !Tok.isContextualKeyword("sending")) &&
        !Tok.isContextualKeyword("consuming") && !Tok.is(tok::kw_repeat) &&
        (!Context.LangOpts.hasFeature(Feature::NonescapableTypes) ||
         !Tok.isContextualKeyword("_resultDependsOn")))
      return true;

    // Parameter specifiers can be an argument label, but they're also
    // contextual keywords, so look ahead one more token (two total) and see
    // if we have a ':' that would
    // indicate that this is an argument label.
    return lookahead<bool>(2, [&](CancellableBacktrackingScope &) {
      if (Tok.is(tok::colon))
        return true; // isolated :

      return Tok.canBeArgumentLabel() && nextTok.is(tok::colon);
    });
  }

  if (isOptionalToken(nextTok)
      || isImplicitlyUnwrappedOptionalToken(nextTok))
    return false;

  // The identifier could be a name or it could be a type. In a closure, we
  // assume it's a name, because the type can be inferred. Elsewhere, we
  // assume it's a type.
  return isClosure;
}

SourceLoc Parser::tryCompleteFunctionParamTypeBeginning() {
  if (!L->isCodeCompletion())
    return SourceLoc();

  // Skip over any starting parameter specifiers.
  {
    CancellableBacktrackingScope backtrack(*this);
    ParsedTypeAttributeList attrs(ParseTypeReason::Unspecified);
    attrs.parse(*this);
    if (!Tok.is(tok::code_complete))
      return SourceLoc();

    backtrack.cancelBacktrack();
  }

  if (CodeCompletionCallbacks)
    CodeCompletionCallbacks->completeTypePossibleFunctionParamBeginning();

  return consumeToken(tok::code_complete);
}

ParserStatus
Parser::parseParameterClause(SourceLoc &leftParenLoc,
                             SmallVectorImpl<ParsedParameter> &params,
                             SourceLoc &rightParenLoc,
                             DefaultArgumentInfo *defaultArgs,
                             ParameterContextKind paramContext) {
  assert(params.empty() && leftParenLoc.isInvalid() &&
         rightParenLoc.isInvalid() && "Must start with empty state");

  // Consume the starting '(';
  leftParenLoc = consumeToken(tok::l_paren);

  // Trivial case: empty parameter list.
  if (Tok.is(tok::r_paren)) {
    rightParenLoc = consumeToken(tok::r_paren);

    // Per SE-0155, enum elements may not have empty parameter lists.
    if (paramContext == ParameterContextKind::EnumElement) {
      decltype(diag::enum_element_empty_arglist) diagnostic;
      if (Context.isSwiftVersionAtLeast(5)) {
        diagnostic = diag::enum_element_empty_arglist;
      } else {
        diagnostic = diag::enum_element_empty_arglist_swift4;
      }

      diagnose(leftParenLoc, diagnostic)
        .highlight({leftParenLoc, rightParenLoc});
      diagnose(leftParenLoc, diag::enum_element_empty_arglist_delete)
        .fixItRemoveChars(leftParenLoc,
                          Lexer::getLocForEndOfToken(SourceMgr, rightParenLoc));
      diagnose(leftParenLoc, diag::enum_element_empty_arglist_add_void)
        .fixItInsertAfter(leftParenLoc, "Void");
    }
    return ParserStatus();
  }

  // Parse the parameter list.
  bool isClosure = paramContext == ParameterContextKind::Closure;
  return parseList(tok::r_paren, leftParenLoc, rightParenLoc,
                      /*AllowSepAfterLast=*/false,
                      diag::expected_rparen_parameter,
                      [&]() -> ParserStatus {
    ParsedParameter param;
    ParserStatus status;
    SourceLoc StartLoc = Tok.getLoc();

    unsigned defaultArgIndex = defaultArgs ? defaultArgs->NextIndex++ : 0;

    // Attributes.
    if (paramContext != ParameterContextKind::EnumElement) {
      auto AttrStatus = parseDeclAttributeList(param.Attrs);
      if (AttrStatus.hasCodeCompletion()) {
        if (this->CodeCompletionCallbacks)
          this->CodeCompletionCallbacks->setAttrTargetDeclKind(DeclKind::Param);
        status.setHasCodeCompletionAndIsError();
      }
    }

    {
      // ('inout' | '__shared' | '__owned' | isolated)?
      bool hasSpecifier = false;
      while (isParameterSpecifier()) {
        // Placing 'inout' in front of the parameter specifiers was allowed in
        // the Swift 2-ish era and got moved to the return type in Swift 3
        // (SE-0031).
        // But new parameters that don't store there location in
        // `SpecifierLoc` were added afterwards and didn't get diagnosed.
        // We thus need to parameter specifiers that don't store their location
        // in `SpecifierLoc` here. `SpecifierLoc` parameters get diagnosed in
        // `validateParameterWithOwnership`

        // is this token the identifier of an argument label? `inout` is a
        // reserved keyword but the other modifiers are not.
        if (!Tok.is(tok::kw_inout)) {
          bool partOfArgumentLabel = lookahead<bool>(1, [&](CancellableBacktrackingScope &) {
            if (Tok.is(tok::colon))
              return true;  // isolated :

            return Tok.canBeArgumentLabel() && peekToken().is(tok::colon);
          });

          if (partOfArgumentLabel)
            break;
        }
        
        if (Tok.isContextualKeyword("isolated")) {
          diagnose(Tok, diag::parameter_specifier_as_attr_disallowed, Tok.getText())
                    .warnUntilSwiftVersion(6);
          // did we already find an 'isolated' type modifier?
          if (param.IsolatedLoc.isValid()) {
            diagnose(Tok, diag::parameter_specifier_repeated)
              .fixItRemove(Tok.getLoc());
            consumeToken();
            continue;
          }

          // consume 'isolated' as type modifier
          param.IsolatedLoc = consumeToken();
          continue;
        }

        if (Tok.isContextualKeyword("_const")) {
          diagnose(Tok, diag::parameter_specifier_as_attr_disallowed, Tok.getText())
                    .warnUntilSwiftVersion(6);
          param.CompileConstLoc = consumeToken();
          continue;
        }

        if (Context.LangOpts.hasFeature(Feature::SendingArgsAndResults) &&
            Tok.isContextualKeyword("sending")) {
          diagnose(Tok, diag::parameter_specifier_as_attr_disallowed,
                   Tok.getText())
              .warnUntilSwiftVersion(6);
          if (param.SendingLoc.isValid()) {
            diagnose(Tok, diag::parameter_specifier_repeated)
                .fixItRemove(Tok.getLoc());
            consumeToken();
            continue;
          }

          param.SendingLoc = consumeToken();
          continue;
        }

        if (!hasSpecifier) {
          // These cases are handled later when mapping to ParamDecls for
          // better fixits.
          if (Tok.is(tok::kw_inout)) {
            param.SpecifierKind = ParamDecl::Specifier::InOut;
            param.SpecifierLoc = consumeToken();
          } else if (Tok.isContextualKeyword("borrowing")) {
            param.SpecifierKind = ParamDecl::Specifier::Borrowing;
            param.SpecifierLoc = consumeToken();
          } else if (Tok.isContextualKeyword("consuming")) {
            param.SpecifierKind = ParamDecl::Specifier::Consuming;
            param.SpecifierLoc = consumeToken();
          } else if (Tok.isContextualKeyword("__shared")) {
            param.SpecifierKind = ParamDecl::Specifier::LegacyShared;
            param.SpecifierLoc = consumeToken();
          } else if (Tok.isContextualKeyword("__owned")) {
            param.SpecifierKind = ParamDecl::Specifier::LegacyOwned;
            param.SpecifierLoc = consumeToken();
          }

          if (param.SendingLoc.isValid()) {
            diagnose(Tok, diag::sending_before_parameter_specifier,
                     getNameForParamSpecifier(param.SpecifierKind));
          }

          hasSpecifier = true;
        } else {
          // Redundant specifiers are fairly common, recognize, reject, and
          // recover from this gracefully.
          diagnose(Tok, diag::parameter_specifier_repeated)
            .fixItRemove(Tok.getLoc());
          consumeToken();
        }
      }
    }
    
    // If let or var is being used as an argument label, allow it but
    // generate a warning.
    if (!isClosure &&
        (Tok.isAny(tok::kw_let, tok::kw_var) ||
         (Context.LangOpts.hasFeature(Feature::ReferenceBindings) &&
          Tok.isAny(tok::kw_inout)))) {
      diagnose(Tok, diag::parameter_let_var_as_attr, Tok.getText())
        .fixItReplace(Tok.getLoc(), "`" + Tok.getText().str() + "`");
    }

    auto parseParamType = [&]() -> ParserResult<TypeRepr> {
      // Currently none of the parameter type completions are relevant for
      // enum cases, so don't include them. We'll complete for a regular type
      // beginning instead.
      if (paramContext != ParameterContextKind::EnumElement) {
        if (auto CCLoc = tryCompleteFunctionParamTypeBeginning()) {
          auto *ET = ErrorTypeRepr::create(Context, CCLoc);
          return makeParserCodeCompletionResult<TypeRepr>(ET);
        }
      }
      return parseType(diag::expected_parameter_type);
    };

    if (startsParameterName(isClosure)) {
      // identifier-or-none for the first name
      param.FirstNameLoc = consumeArgumentLabel(param.FirstName,
                                                /*diagnoseDollarPrefix=*/!isClosure);

      // identifier-or-none? for the second name
      if (Tok.canBeArgumentLabel())
        param.SecondNameLoc = consumeArgumentLabel(param.SecondName,
                                                   /*diagnoseDollarPrefix=*/true);

      // Operators, closures, and enum elements cannot have API names.
      if ((paramContext == ParameterContextKind::Operator ||
           paramContext == ParameterContextKind::Closure ||
           paramContext == ParameterContextKind::EnumElement) &&
          !param.FirstName.empty() &&
          param.SecondNameLoc.isValid()) {
        enum KeywordArgumentDiagnosticContextKind {
          Operator    = 0,
          Closure     = 1,
          EnumElement = 2,
        } diagContextKind;

        switch (paramContext) {
        case ParameterContextKind::Operator:
          diagContextKind = Operator;
          break;
        case ParameterContextKind::Closure:
          diagContextKind = Closure;
          break;
        case ParameterContextKind::EnumElement:
          diagContextKind = EnumElement;
          break;
        default:
          llvm_unreachable("Unhandled parameter context kind!");
        }
        diagnose(param.FirstNameLoc, diag::parameter_operator_keyword_argument,
                 unsigned(diagContextKind))
          .fixItRemoveChars(param.FirstNameLoc, param.SecondNameLoc);
        param.FirstName = param.SecondName;
        param.FirstNameLoc = param.SecondNameLoc;
        param.SecondName = Identifier();
        param.SecondNameLoc = SourceLoc();
      }

      // (':' type)?
      if (consumeIf(tok::colon)) {

        auto type = parseParamType();
        status |= type;
        param.Type = type.getPtrOrNull();

        // If we didn't parse a type, then we already diagnosed that the type
        // was invalid.  Remember that.
        if (type.isNull() && !type.hasCodeCompletion())
          param.isInvalid = true;
      } else if (paramContext != Parser::ParameterContextKind::Closure) {
        diagnose(Tok, diag::expected_parameter_colon);
        param.isInvalid = true;
      }
    } else {
      // Otherwise, we have invalid code.  Check to see if this looks like a
      // type.  If so, diagnose it as a common error.
      bool isBareType = false;
      {
        BacktrackingScope backtrack(*this);
        isBareType = canParseType() && Tok.isAny(tok::comma, tok::r_paren,
                                                 tok::equal);
      }

      if (isBareType && paramContext == ParameterContextKind::EnumElement) {
        auto type = parseParamType();
        status |= type;
        param.Type = type.getPtrOrNull();
        param.FirstName = Identifier();
        param.FirstNameLoc = SourceLoc();
        param.SecondName = Identifier();
        param.SecondNameLoc = SourceLoc();
      } else if (isBareType && !Tok.is(tok::code_complete)) {
        // Otherwise, if this is a bare type, then the user forgot to name the
        // parameter, e.g. "func foo(Int) {}"
        // Don't enter this case if the element could only be parsed as a bare
        // type because a code completion token is positioned here. In this case
        // the user is about to type the parameter label and we shouldn't
        // suggest types.
        SourceLoc typeStartLoc = Tok.getLoc();
        auto type = parseParamType();
        status |= type;
        param.Type = type.getPtrOrNull();

        // If this is a closure declaration, what is going
        // on is most likely argument destructuring, we are going
        // to diagnose that after all of the parameters are parsed.
        if (param.Type) {
          // Mark current parameter type as invalid so it is possible
          // to diagnose it as destructuring of the closure parameter list.
          param.isPotentiallyDestructured = true;
          if (!isClosure) {
            // Unnamed parameters must be written as "_: Type".
            diagnose(typeStartLoc, diag::parameter_unnamed)
                .fixItInsert(typeStartLoc, "_: ");
          } else {
            // Unnamed parameters were accidentally possibly accepted after
            // SE-110 depending on the kind of declaration.  We now need to
            // warn about the misuse of this syntax and offer to
            // fix it.
            // An exception to this rule is when the type is declared with type sugar
            // Reference: https://github.com/apple/swift/issues/54133
            if (isa<OptionalTypeRepr>(param.Type)
                || isa<ImplicitlyUnwrappedOptionalTypeRepr>(param.Type)) {
                diagnose(typeStartLoc, diag::parameter_unnamed)
                    .fixItInsert(typeStartLoc, "_: ");
            } else {
                diagnose(typeStartLoc, diag::parameter_unnamed)
                    .warnUntilSwiftVersion(6)
                    .fixItInsert(typeStartLoc, "_: ");
            }
          }
        }
      } else {
        // Otherwise, we're not sure what is going on, but this doesn't smell
        // like a parameter.
        diagnose(Tok, diag::expected_parameter_name);
        param.isInvalid = true;
        param.FirstNameLoc = Tok.getLoc();
        TokReceiver->registerTokenKindChange(param.FirstNameLoc,
                                             tok::identifier);
        status.setIsParseError();
      }
    }

    // If this parameter had an ellipsis, check it has a TypeRepr.
    if (Tok.isEllipsis()) {
      if (param.Type == nullptr && !param.isInvalid) {
        diagnose(Tok, diag::untyped_pattern_ellipsis);
        consumeToken();
      }
    }

    // ('=' expr) or ('==' expr)?
    bool isEqualBinaryOperator =
        Tok.isBinaryOperator() && Tok.getText() == "==";
    if (Tok.is(tok::equal) || isEqualBinaryOperator) {
      SourceLoc EqualLoc = Tok.getLoc();

      if (isEqualBinaryOperator) {
        diagnose(Tok, diag::expected_assignment_instead_of_comparison_operator)
            .fixItReplace(EqualLoc, "=");
      }

      status |= parseDefaultArgument(
          *this, defaultArgs, defaultArgIndex, param.DefaultArg,
          param.hasInheritedDefaultArg, paramContext);
    }

    // If we haven't made progress, don't add the parameter.
    if (Tok.getLoc() == StartLoc) {
      // If we took a default argument index for this parameter, but didn't add
      // one, then give it back.
      if (defaultArgs) --defaultArgs->NextIndex;
      return status;
    }

    params.push_back(param);
    return status;
  });
}

static TypeRepr *
validateParameterWithOwnership(Parser &parser,
                               Parser::ParsedParameter &paramInfo,
                               ParamSpecifier specifier,
                               bool parsingEnumElt) {
  auto type = paramInfo.Type;
  auto loc = paramInfo.SpecifierLoc;
  // If we're validating an enum element, 'inout' is not allowed
  // at all - Sema will catch this for us.  In all other contexts, we
  // assume the user put 'inout' in the wrong place and offer a fixit.
  if (parsingEnumElt) {
    return new (parser.Context) OwnershipTypeRepr(type, specifier, loc);
  }
  
  if (isa<SpecifierTypeRepr>(type)) {
    parser.diagnose(loc, diag::parameter_specifier_repeated).fixItRemove(loc);
  } else {
    auto specifierName = ParamDecl::getSpecifierSpelling(specifier);
    llvm::SmallString<128> replacement(specifierName);
    replacement += " ";
    parser
        .diagnose(loc, diag::parameter_specifier_as_attr_disallowed,
                  specifierName)
        .fixItRemove(loc)
        .fixItInsert(type->getStartLoc(), replacement);
    type = new (parser.Context) OwnershipTypeRepr(type, specifier, loc);
  }

  return type;
}

/// Map parsed parameters to a ParameterList.
static ParameterList *
mapParsedParameters(Parser &parser,
                    SourceLoc leftParenLoc,
                    MutableArrayRef<Parser::ParsedParameter> params,
                    SourceLoc rightParenLoc,
                    SmallVectorImpl<Identifier> *argNames,
                    Parser::ParameterContextKind paramContext) {
  auto &ctx = parser.Context;

  // Local function to create a pattern for a single parameter.
  auto createParam = [&](Parser::ParsedParameter &paramInfo,
                         Identifier argName, SourceLoc argNameLoc,
                         Identifier paramName, SourceLoc paramNameLoc)
  -> ParamDecl * {
    auto param = new (ctx) ParamDecl(paramInfo.SpecifierLoc,
                                     argNameLoc, argName,
                                     paramNameLoc, paramName,
                                     parser.CurDeclContext);
    param->getAttrs() = paramInfo.Attrs;

    bool parsingEnumElt
      = (paramContext == Parser::ParameterContextKind::EnumElement);
    // If we're not parsing an enum case, lack of a SourceLoc for both
    // names indicates the parameter is synthetic.
    if (!parsingEnumElt && argNameLoc.isInvalid() && paramNameLoc.isInvalid())
      param->setImplicit();
    
    // If we diagnosed this parameter as a parse error, propagate to the decl.
    if (paramInfo.isInvalid)
      param->setInvalid();

    // If we need to diagnose this parameter as a destructuring, propagate that
    // to the decl.
    // FIXME: This is a terrible way to catch this.
    if (paramInfo.isPotentiallyDestructured)
      param->setDestructured(true);

    // If a type was provided, create the type for the parameter.
    if (auto type = paramInfo.Type) {
      // If 'inout' was specified, turn the type into an in-out type.
      if (paramInfo.SpecifierKind != ParamDecl::Specifier::Default) {
        type = validateParameterWithOwnership(parser, paramInfo,
                                              paramInfo.SpecifierKind,
                                              parsingEnumElt);
      }

      if (paramInfo.IsolatedLoc.isValid()) {
        type = new (parser.Context) IsolatedTypeRepr(
            type, paramInfo.IsolatedLoc);
        param->setIsolated();
      }

      if (paramInfo.CompileConstLoc.isValid()) {
        type = new (parser.Context) CompileTimeConstTypeRepr(
            type, paramInfo.CompileConstLoc);
        param->setCompileTimeConst();
      }

      if (paramInfo.ResultDependsOnLoc.isValid()) {
        type = new (parser.Context)
            ResultDependsOnTypeRepr(type, paramInfo.ResultDependsOnLoc);
        param->setResultDependsOn();
      }

      if (paramInfo.SendingLoc.isValid()) {
        type = new (parser.Context) SendingTypeRepr(type, paramInfo.SendingLoc);
        param->setSending();
      }

      param->setTypeRepr(type);

      // Dig through the type to find any attributes or modifiers that are
      // associated with the type but should also be reflected on the
      // declaration.
      {
        auto unwrappedType = type;
        while (true) {
          if (auto *ATR = dyn_cast<AttributedTypeRepr>(unwrappedType)) {
            // At this point we actually don't know if that's valid to mark
            // this parameter declaration as `autoclosure` because type has
            // not been resolved yet - it should either be a function type
            // or typealias with underlying function type.
            if (ATR->has(TypeAttrKind::Autoclosure))
              param->setAutoClosure(true);

            unwrappedType = ATR->getTypeRepr();
            continue;
          }

          if (auto *STR = dyn_cast<SpecifierTypeRepr>(unwrappedType)) {
            if (isa<IsolatedTypeRepr>(STR))
              param->setIsolated(true);
            else if (isa<CompileTimeConstTypeRepr>(STR))
              param->setCompileTimeConst(true);
            else if (isa<ResultDependsOnTypeRepr>(STR))
              param->setResultDependsOn(true);
            else if (isa<SendingTypeRepr>(STR))
              param->setSending(true);
            unwrappedType = STR->getBase();
            continue;
          }

          break;
        }
      }
    } else if (paramInfo.SpecifierLoc.isValid()) {
      llvm::SmallString<16> specifier;
      {
        llvm::raw_svector_ostream ss(specifier);
        
        ss << '\'' << ParamDecl::getSpecifierSpelling(paramInfo.SpecifierKind)
           << '\'';
      }
      parser.diagnose(paramInfo.SpecifierLoc, diag::specifier_must_have_type,
                      specifier);
      paramInfo.SpecifierLoc = SourceLoc();
      paramInfo.SpecifierKind = ParamDecl::Specifier::Default;

      param->setSpecifier(ParamSpecifier::Default);
    } else {
      param->setSpecifier(ParamSpecifier::Default);
    }

    return param;
  };

  // Collect the elements of the tuple patterns for argument and body
  // parameters.
  SmallVector<ParamDecl*, 4> elements;

  for (auto &param : params) {
    // Whether the provided name is API by default depends on the parameter
    // context.
    bool isKeywordArgumentByDefault;
    switch (paramContext) {
    case Parser::ParameterContextKind::Closure:
    case Parser::ParameterContextKind::Subscript:
    case Parser::ParameterContextKind::Operator:
      isKeywordArgumentByDefault = false;
      break;
    case Parser::ParameterContextKind::EnumElement:
    case Parser::ParameterContextKind::Curried:
    case Parser::ParameterContextKind::Initializer:
    case Parser::ParameterContextKind::Function:
    case Parser::ParameterContextKind::Macro:
      isKeywordArgumentByDefault = true;
      break;
    }

    // Create the pattern.
    ParamDecl *result = nullptr;
    Identifier argName;
    Identifier paramName;
    if (param.SecondNameLoc.isValid()) {
      argName = param.FirstName;
      paramName = param.SecondName;

      // Both names were provided, so pass them in directly.
      result = createParam(param, argName, param.FirstNameLoc,
                           paramName, param.SecondNameLoc);

      // If the first and second names are equivalent and non-empty, and we
      // would have an argument label by default, complain.
      if (isKeywordArgumentByDefault && param.FirstName == param.SecondName
          && !param.FirstName.empty()) {
        parser.diagnose(param.FirstNameLoc,
                        diag::parameter_extraneous_double_up,
                        param.FirstName)
          .fixItRemoveChars(param.FirstNameLoc, param.SecondNameLoc);
      }
    } else {
      if (isKeywordArgumentByDefault)
        argName = param.FirstName;
      paramName = param.FirstName;

      result = createParam(param, argName, SourceLoc(),
                           param.FirstName, param.FirstNameLoc);
    }

    assert (((!param.DefaultArg &&
              !param.hasInheritedDefaultArg) ||
             paramContext == Parser::ParameterContextKind::Function ||
             paramContext == Parser::ParameterContextKind::Operator ||
             paramContext == Parser::ParameterContextKind::Initializer ||
             paramContext == Parser::ParameterContextKind::EnumElement ||
             paramContext == Parser::ParameterContextKind::Subscript ||
             paramContext == Parser::ParameterContextKind::Macro) &&
            "Default arguments are only permitted on the first param clause");

    if (param.DefaultArg) {
      DefaultArgumentKind kind = getDefaultArgKind(param.DefaultArg);
      result->setDefaultArgumentKind(kind);
      result->setDefaultExpr(param.DefaultArg, /*isTypeChecked*/ false);
    } else if (param.hasInheritedDefaultArg) {
      result->setDefaultArgumentKind(DefaultArgumentKind::Inherited);
    }

    elements.push_back(result);

    if (argNames)
      argNames->push_back(argName);
  }

  return ParameterList::create(ctx, leftParenLoc, elements, rightParenLoc);
}

/// Parse a single parameter-clause.
ParserResult<ParameterList>
Parser::parseSingleParameterClause(ParameterContextKind paramContext,
                                   SmallVectorImpl<Identifier> *namePieces,
                                   DefaultArgumentInfo *defaultArgs) {
  if (!Tok.is(tok::l_paren)) {
    // If we don't have the leading '(', complain.
    Diag<> diagID;
    switch (paramContext) {
    case ParameterContextKind::Function:
    case ParameterContextKind::Operator:
      diagID = diag::func_decl_without_paren;
      break;
    case ParameterContextKind::Subscript:
      diagID = diag::expected_lparen_subscript;
      break;
    case ParameterContextKind::Initializer:
      diagID = diag::expected_lparen_initializer;
      break;
    case ParameterContextKind::Macro:
      diagID = diag::expected_lparen_macro;
      break;
    case ParameterContextKind::EnumElement:
    case ParameterContextKind::Closure:
    case ParameterContextKind::Curried:
      llvm_unreachable("should never be here");
    }

    {
      auto diag = diagnose(Tok, diagID);
      if (Tok.isAny(tok::l_brace, tok::arrow, tok::kw_throws, tok::kw_rethrows))
        diag.fixItInsertAfter(PreviousLoc, "()");
    }

    // Create an empty parameter list to recover.
    return makeParserErrorResult(
        ParameterList::createEmpty(Context, PreviousLoc, PreviousLoc));
  }

  ParserStatus status;
  SmallVector<ParsedParameter, 4> params;
  SourceLoc leftParenLoc, rightParenLoc;
  
  // Parse the parameter clause.
  status |= parseParameterClause(leftParenLoc, params, rightParenLoc,
                                 defaultArgs, paramContext);

  // Turn the parameter clause into argument and body patterns.
  auto paramList = mapParsedParameters(*this, leftParenLoc, params,
                                       rightParenLoc, namePieces, paramContext);

  return makeParserResult(status, paramList);
}

/// Parse function arguments.
///
/// Emits a special diagnostic if there are multiple parameter lists,
/// but otherwise is identical to parseSingleParameterClause().
ParserStatus
Parser::parseFunctionArguments(SmallVectorImpl<Identifier> &NamePieces,
                               ParameterList *&BodyParams,
                               ParameterContextKind paramContext,
                               DefaultArgumentInfo &DefaultArgs) {
  // Parse parameter-clauses.
  ParserStatus status;

  auto FirstParameterClause
    = parseSingleParameterClause(paramContext, &NamePieces, &DefaultArgs);
  status |= FirstParameterClause;
  BodyParams = FirstParameterClause.get();

  bool MultipleParameterLists = false;
  while (Tok.is(tok::l_paren)) {
    MultipleParameterLists = true;
    auto CurriedParameterClause
      = parseSingleParameterClause(ParameterContextKind::Curried);
    status |= CurriedParameterClause;
  }

  // If the decl uses currying syntax, complain that syntax has gone away.
  if (MultipleParameterLists) {
    diagnose(BodyParams->getStartLoc(),
             diag::parameter_curry_syntax_removed);
  }

  return status;
}

/// Parse a function definition signature.
///   func-signature:
///     func-arguments ('async'|'reasync')? func-throws? func-signature-result?
///   func-signature-result:
///     '->' type
///
/// Note that this leaves retType as null if unspecified.
ParserStatus
Parser::parseFunctionSignature(DeclBaseName SimpleName,
                               DeclName &FullName,
                               ParameterList *&bodyParams,
                               DefaultArgumentInfo &defaultArgs,
                               SourceLoc &asyncLoc,
                               bool &reasync,
                               SourceLoc &throwsLoc,
                               bool &rethrows,
                               TypeRepr *&thrownType,
                               TypeRepr *&retType) {
  SmallVector<Identifier, 4> NamePieces;
  ParserStatus Status;

  ParameterContextKind paramContext =
      SimpleName.isOperator()
          ? ParameterContextKind::Operator
          : (SimpleName.isConstructor() ? ParameterContextKind::Initializer
                                        : ParameterContextKind::Function);
  Status |= parseFunctionArguments(NamePieces, bodyParams, paramContext,
                                   defaultArgs);
  FullName = DeclName(Context, SimpleName, NamePieces);

  // Check for the 'async' and 'throws' keywords.
  reasync = false;
  rethrows = false;
  thrownType = nullptr;
  Status |= parseEffectsSpecifiers(SourceLoc(),
                                   asyncLoc, &reasync,
                                   throwsLoc, &rethrows, thrownType);

  // If there's a trailing arrow, parse the rest as the result type.
  SourceLoc arrowLoc;
  if (Tok.isAny(tok::arrow, tok::colon)) {
    if (!consumeIf(tok::arrow, arrowLoc)) {
      // FixIt ':' to '->'.
      diagnose(Tok, diag::func_decl_expected_arrow)
          .fixItReplace(Tok.getLoc(), " -> ");
      arrowLoc = consumeToken(tok::colon);
    }

    // Check for effect specifiers after the arrow, but before the return type,
    // and correct it.
    parseEffectsSpecifiers(arrowLoc, asyncLoc, &reasync, throwsLoc, &rethrows,
                           thrownType);

    ParserResult<TypeRepr> ResultType =
        parseDeclResultType(diag::expected_type_function_result);
    retType = ResultType.getPtrOrNull();
    Status |= ResultType;
    if (Status.isErrorOrHasCompletion())
      return Status;

    // Check for effect specifiers after the type and correct it.
    parseEffectsSpecifiers(
        arrowLoc, asyncLoc, &reasync, throwsLoc, &rethrows, thrownType);
  } else {
    // Otherwise, we leave retType null.
    retType = nullptr;
  }

  return Status;
}

bool Parser::isThrowsEffectSpecifier(const Token &T) {
  return T.isAny(tok::kw_throws, tok::kw_rethrows) ||
    (T.isAny(tok::kw_throw, tok::kw_try) && !T.isAtStartOfLine());
}

bool Parser::isEffectsSpecifier(const Token &T) {
  // NOTE: If this returns 'true', that token must be handled in
  //       'parseEffectsSpecifiers()'.

  if (T.isContextualKeyword("async") ||
      (T.isContextualKeyword("await") && !T.isAtStartOfLine()) ||
      T.isContextualKeyword("reasync"))
    return true;

  if (isThrowsEffectSpecifier(T))
    return true;

  return false;
}

ParserStatus Parser::parseEffectsSpecifiers(SourceLoc existingArrowLoc,
                                            SourceLoc &asyncLoc,
                                            bool *reasync,
                                            SourceLoc &throwsLoc,
                                            bool *rethrows,
                                            TypeRepr *&thrownType) {
  ParserStatus status;
  while (true) {
    // 'async'
    bool isReasync = (shouldParseExperimentalConcurrency() &&
                      Tok.isContextualKeyword("reasync"));
    if (Tok.isContextualKeyword("async") ||
        isReasync) {
      if (asyncLoc.isValid()) {
        diagnose(Tok, diag::duplicate_effects_specifier, Tok.getText())
            .highlight(asyncLoc)
            .fixItRemove(Tok.getLoc());
      } else if (!reasync && isReasync) {
        // Replace 'reasync' with 'async' unless it's allowed.
        diagnose(Tok, diag::reasync_function_type)
            .fixItReplace(Tok.getLoc(), "async");
      } else if (existingArrowLoc.isValid()) {
        SourceLoc insertLoc = existingArrowLoc;
        if (throwsLoc.isValid() &&
            SourceMgr.isBeforeInBuffer(throwsLoc, insertLoc))
          insertLoc = throwsLoc;
        diagnose(Tok, diag::async_or_throws_in_wrong_position,
                 (reasync && isReasync) ? "reasync" : "async")
            .fixItRemove(Tok.getLoc())
            .fixItInsert(insertLoc,
                         (reasync && isReasync) ? "reasync " : "async ");
      } else if (throwsLoc.isValid()) {
        // 'async' cannot be after 'throws'.
        assert(existingArrowLoc.isInvalid());
        diagnose(Tok, diag::async_after_throws,
                 reasync && isReasync,
                 rethrows && *rethrows)
            .fixItRemove(Tok.getLoc())
            .fixItInsert(throwsLoc, isReasync ? "reasync " : "async ");
      }
      if (asyncLoc.isInvalid()) {
        Tok.setKind(tok::contextual_keyword);
        if (reasync)
          *reasync = isReasync;
        asyncLoc = Tok.getLoc();
      }
      consumeToken();
      continue;
    }
    // diagnose 'await'
    if (Tok.isContextualKeyword("await") && !Tok.isAtStartOfLine()) {
      diagnose(Tok, diag::await_in_function_type)
        .fixItReplace(Tok.getLoc(), "async");
      consumeToken();
      continue;
    }

    // 'throws'/'rethrows', or diagnose 'throw'/'try'.
    if (isThrowsEffectSpecifier(Tok)) {
      bool isRethrows = Tok.is(tok::kw_rethrows);

      if (throwsLoc.isValid()) {
        diagnose(Tok, diag::duplicate_effects_specifier, Tok.getText())
            .highlight(throwsLoc)
            .fixItRemove(Tok.getLoc());
      } else if (Tok.isAny(tok::kw_throw, tok::kw_try)) {
        // Replace 'throw' or 'try' with 'throws'.
        diagnose(Tok, diag::throw_in_function_type)
            .fixItReplace(Tok.getLoc(), "throws");
      } else if (!rethrows && isRethrows) {
        // Replace 'rethrows' with 'throws' unless it's allowed.
        diagnose(Tok, diag::rethrowing_function_type)
            .fixItReplace(Tok.getLoc(), "throws");
      } else if (existingArrowLoc.isValid()) {
        diagnose(Tok, diag::async_or_throws_in_wrong_position, Tok.getText())
            .fixItRemove(Tok.getLoc())
            .fixItInsert(existingArrowLoc, (Tok.getText() + " ").str());
      }

      if (throwsLoc.isInvalid()) {
        if (rethrows)
          *rethrows = isRethrows;
        throwsLoc = Tok.getLoc();
      }
      consumeToken();

      // Parse the thrown error type.
      SourceLoc lParenLoc;
      if (consumeIf(tok::l_paren, lParenLoc)) {
        ParserResult<TypeRepr> parsedThrownTy =
            parseType(diag::expected_thrown_error_type);
        thrownType = parsedThrownTy.getPtrOrNull();
        status |= parsedThrownTy;

        SourceLoc rParenLoc;
        parseMatchingToken(
            tok::r_paren, rParenLoc,
            diag::expected_rparen_after_thrown_error_type, lParenLoc);

        if (isRethrows) {
          diagnose(throwsLoc, diag::rethrows_with_thrown_error)
            .highlight(SourceRange(lParenLoc, rParenLoc));

          isRethrows = false;

          if (rethrows)
            *rethrows = false;
        }
      }

      continue;
    }

    // Code completion.
    if (Tok.is(tok::code_complete) && !Tok.isAtStartOfLine() &&
        !existingArrowLoc.isValid()) {
      if (CodeCompletionCallbacks) {
        CodeCompletionCallbacks->completeEffectsSpecifier(asyncLoc.isValid(),
                                                          throwsLoc.isValid());
      }
      consumeToken(tok::code_complete);
      status.setHasCodeCompletionAndIsError();
      continue;
    }

    break;
  }
  return status;
}

/// Parse a pattern with an optional type annotation.
///
///  typed-pattern ::= pattern (':' type)?
///
ParserResult<Pattern> Parser::parseTypedPattern() {
  auto result = parsePattern();
  
  // Now parse an optional type annotation.
  if (Tok.is(tok::colon)) {
    SourceLoc colonLoc = consumeToken(tok::colon);
    
    if (result.isNull()) {
      // Recover by creating AnyPattern.
      auto *AP = new (Context) AnyPattern(colonLoc);
      if (colonLoc.isInvalid())
        AP->setImplicit();
      result = makeParserErrorResult(AP);
    }

    ParserResult<TypeRepr> Ty = parseDeclResultType(diag::expected_type);
    if (Ty.hasCodeCompletion())
      return makeParserCodeCompletionResult<Pattern>();
    if (!Ty.isNull()) {
      // Attempt to diagnose initializer calls incorrectly written
      // as typed patterns, such as "var x: [Int]()".
      // Disable this tentative parse when in IDE inspection mode, otherwise
      // code-completion may enter the delayed-decl state twice.
      if (Tok.isFollowingLParen() &&
          !SourceMgr.hasIDEInspectionTargetBuffer()) {
        CancellableBacktrackingScope backtrack(*this);

        SmallVector<ExprListElt, 2> elts;
        auto argListResult = parseArgumentList(tok::l_paren, tok::r_paren,
                                               /*isExprBasic*/ false);
        if (!argListResult.isParseErrorOrHasCompletion()) {
          backtrack.cancelBacktrack();
          
          // Suggest replacing ':' with '='
          auto *args = argListResult.get();
          diagnose(args->getLParenLoc(), diag::initializer_as_typed_pattern)
            .highlight({Ty.get()->getStartLoc(), args->getRParenLoc()})
            .fixItReplace(colonLoc, " = ");
          result.setIsParseError();
        }
      }
    } else {
      Ty = makeParserResult(ErrorTypeRepr::create(Context, PreviousLoc));
    }
    
    result = makeParserResult(result,
                            new (Context) TypedPattern(result.get(), Ty.get()));
  }
  
  return result;
}

/// Parse a pattern.
///   pattern ::= identifier
///   pattern ::= '_'
///   pattern ::= pattern-tuple
///   pattern ::= 'var' pattern
///   pattern ::= 'let' pattern
///
ParserResult<Pattern> Parser::parsePattern() {
  auto introducer =
      InBindingPattern.getIntroducer().value_or(VarDecl::Introducer::Let);
  switch (Tok.getKind()) {
  case tok::l_paren:
    return parsePatternTuple();

  case tok::kw__: {
    // Normally, '_' is invalid in type context for patterns, but they show up
    // in interface files as the name for type members that are non-public.
    // Treat them as an implicitly synthesized NamedPattern with a nameless
    // VarDecl inside.
    if (CurDeclContext->isTypeContext() &&
        SF.Kind == SourceFileKind::Interface) {
      auto VD = new (Context) VarDecl(
        /*IsStatic*/false, introducer,
        consumeToken(tok::kw__), Identifier(), CurDeclContext);
      return makeParserResult(NamedPattern::createImplicit(Context, VD));
    }

    const auto isAsyncLet =
        InPatternWithAsyncAttribute && introducer == VarDecl::Introducer::Let;
    return makeParserResult(
        new (Context) AnyPattern(consumeToken(tok::kw__), isAsyncLet));
  }
  case tok::identifier: {
    Identifier name;
    SourceLoc loc = consumeIdentifier(name, /*diagnoseDollarPrefix=*/true);
    if (Tok.isIdentifierOrUnderscore() && !Tok.isContextualDeclKeyword() &&
        !Tok.isAtStartOfLine())
      diagnoseConsecutiveIDs(name.str(), loc,
                             introducer == VarDecl::Introducer::Let
                             ? "constant" : "variable");

    return makeParserResult(createBindingFromPattern(loc, name, introducer));
  }
    
  case tok::code_complete:
    if (!CurDeclContext->isTypeContext()) {
      // This cannot be an overridden property, so just eat the token. We cannot
      // code complete anything here -- we expect an identifier.
      consumeToken(tok::code_complete);
    }
    return makeParserCodeCompletionStatus();
  case tok::kw_inout:
    // If we don't have the reference binding feature, break if we have
    // inout. Otherwise, go below.
    if (!Context.LangOpts.hasFeature(Feature::ReferenceBindings))
      break;
    LLVM_FALLTHROUGH;
  case tok::kw_var:
  case tok::kw_let: {
    auto newBindingState = PatternBindingState(Tok);
    SourceLoc varLoc = consumeToken();

    // 'var', 'let', 'inout' patterns shouldn't nest.
    if (InBindingPattern.getIntroducer().has_value()) {
      auto diag = diag::var_pattern_in_var;
      unsigned index = *newBindingState.getSelectIndexForIntroducer();
      if (Context.LangOpts.hasFeature(Feature::ReferenceBindings)) {
        diag = diag::var_pattern_in_var_inout;
      }
      diagnose(varLoc, diag, index);
    }

    // 'let' isn't valid inside an implicitly immutable context, but var is.
    if (newBindingState.isLet() &&
        InBindingPattern == PatternBindingState::ImplicitlyImmutable)
      diagnose(varLoc, diag::let_pattern_in_immutable_context);

    // In our recursive parse, remember that we're in a let/var/inout
    // pattern. We default to var if we don't have an immediate pattern bidning
    // state.
    llvm::SaveAndRestore<decltype(InBindingPattern)> T(
        InBindingPattern, newBindingState.getPatternBindingStateForIntroducer(
                              VarDecl::Introducer::Var));

    // Reset async attribute in parser context.
    llvm::SaveAndRestore<bool> AsyncAttr(InPatternWithAsyncAttribute, false);

    ParserResult<Pattern> subPattern = parsePattern();
    if (subPattern.hasCodeCompletion())
      return makeParserCodeCompletionResult<Pattern>();
    if (subPattern.isNull())
      return nullptr;
    return makeParserResult(new (Context) BindingPattern(
        varLoc,
        newBindingState.getIntroducer().value_or(VarDecl::Introducer::Var),
        subPattern.get()));
  }
      
  default:
    break;
  }

  // Handle the default case.
  if (Tok.isKeyword() &&
      (peekToken().is(tok::colon) || peekToken().is(tok::equal))) {
    diagnose(Tok, diag::keyword_cant_be_identifier, Tok.getText());
    diagnose(Tok, diag::backticks_to_escape)
      .fixItReplace(Tok.getLoc(), "`" + Tok.getText().str() + "`");
    SourceLoc Loc = Tok.getLoc();
    consumeToken();
    return makeParserErrorResult(new (Context) AnyPattern(Loc));
  }
  diagnose(Tok, diag::expected_pattern);
  return nullptr;
}

Pattern *Parser::createBindingFromPattern(SourceLoc loc, Identifier name,
                                          VarDecl::Introducer introducer) {
  auto var = new (Context) VarDecl(/*IsStatic*/false, introducer,
                                   loc, name, CurDeclContext);
  return new (Context) NamedPattern(var);
}

/// Parse an element of a tuple pattern.
///
///   pattern-tuple-element:
///     (identifier ':')? pattern
std::pair<ParserStatus, std::optional<TuplePatternElt>>
Parser::parsePatternTupleElement() {
  // If this element has a label, parse it.
  Identifier Label;
  SourceLoc LabelLoc;

  // If the tuple element has a label, parse it.
  if (Tok.is(tok::identifier) && peekToken().is(tok::colon)) {
    LabelLoc = consumeIdentifier(Label, /*diagnoseDollarPrefix=*/true);
    consumeToken(tok::colon);
  }

  // Parse the pattern.
  ParserResult<Pattern>  pattern = parsePattern();
  if (pattern.hasCodeCompletion())
    return std::make_pair(makeParserCodeCompletionStatus(), std::nullopt);
  if (pattern.isNull())
    return std::make_pair(makeParserError(), std::nullopt);

  auto Elt = TuplePatternElt(Label, LabelLoc, pattern.get());
  return std::make_pair(makeParserSuccess(), Elt);
}

/// Parse a tuple pattern.
///
///   pattern-tuple:
///     '(' pattern-tuple-body? ')'
///   pattern-tuple-body:
///     pattern-tuple-element (',' pattern-tuple-body)*
ParserResult<Pattern> Parser::parsePatternTuple() {
  StructureMarkerRAII ParsingPatternTuple(*this, Tok);
  SourceLoc LPLoc = consumeToken(tok::l_paren);
  SourceLoc RPLoc;

  // Parse all the elements.
  SmallVector<TuplePatternElt, 8> elts;
  ParserStatus ListStatus =
    parseList(tok::r_paren, LPLoc, RPLoc,
              /*AllowSepAfterLast=*/false,
              diag::expected_rparen_tuple_pattern_list,
              [&] () -> ParserStatus {
    // Parse the pattern tuple element.
    ParserStatus EltStatus;
    std::optional<TuplePatternElt> elt;
    std::tie(EltStatus, elt) = parsePatternTupleElement();
    if (EltStatus.hasCodeCompletion())
      return makeParserCodeCompletionStatus();
    if (!elt)
      return makeParserError();

    // Add this element to the list.
    elts.push_back(*elt);
    return makeParserSuccess();
  });

  return makeParserResult(
           ListStatus,
           TuplePattern::createSimple(Context, LPLoc, elts, RPLoc));
}

/// Parse an optional type annotation on a pattern.
///
///  pattern-type-annotation ::= (':' type)?
///
ParserResult<Pattern> Parser::
parseOptionalPatternTypeAnnotation(ParserResult<Pattern> result) {
  if (!Tok.is(tok::colon))
    return result;

  // Parse an optional type annotation.
  consumeToken(tok::colon);

  if (result.isNull())
    return result;

  Pattern *P = result.get();
  ParserStatus status;
  if (result.hasCodeCompletion())
    status.setHasCodeCompletionAndIsError();

  ParserResult<TypeRepr> Ty = parseType();
  if (Ty.hasCodeCompletion()) {
    result.setHasCodeCompletionAndIsError();
    return result;
  }

  TypeRepr *repr = Ty.getPtrOrNull();
  if (!repr)
    repr = ErrorTypeRepr::create(Context, PreviousLoc);

  return makeParserResult(status, new (Context) TypedPattern(P, repr));
}



/// matching-pattern ::= 'is' type
/// matching-pattern ::= matching-pattern-var
/// matching-pattern ::= expr
///
ParserResult<Pattern> Parser::parseMatchingPattern(bool isExprBasic) {
  // TODO: Since we expect a pattern in this position, we should optimistically
  // parse pattern nodes for productions shared by pattern and expression
  // grammar. For short-term ease of initial implementation, we always go
  // through the expr parser for ambiguous productions.

  // Parse productions that can only be patterns.
  if (Tok.isAny(tok::kw_var, tok::kw_let) ||
      (Context.LangOpts.hasFeature(Feature::ReferenceBindings) &&
       Tok.isAny(tok::kw_inout))) {
    assert(Tok.isAny(tok::kw_let, tok::kw_var, tok::kw_inout) && "expects var or let");
    auto newPatternBindingState = PatternBindingState(Tok);
    SourceLoc varLoc = consumeToken();

    return parseMatchingPatternAsBinding(newPatternBindingState, varLoc,
                                         isExprBasic);
  }
  
  // The `borrowing` modifier is a contextual keyword, so it's only accepted
  // directly applied to a binding name, as in `case .foo(borrowing x)`.
  if ((Tok.isContextualKeyword("_borrowing")
       || Tok.isContextualKeyword("borrowing"))
      && peekToken().isAny(tok::identifier, tok::kw_self, tok::dollarident,
                           tok::code_complete)
      && !peekToken().isAtStartOfLine()) {
    diagnose(Tok.getLoc(),
             diag::borrowing_syntax_change)
      .fixItReplace(Tok.getLoc(), "let");

    Tok.setKind(tok::contextual_keyword);
    SourceLoc borrowingLoc = consumeToken();
    
    // If we have `case borrowing x.`, `x(`, `x[`, or `x<` then this looks
    // like an attempt to include a subexpression under a `borrowing`
    // binding, which isn't yet supported.
    if (peekToken().isAny(tok::period, tok::period_prefix, tok::l_paren,
                          tok::l_square)
        || (peekToken().isAnyOperator() && peekToken().getText().equals("<"))) {

      // Diagnose the unsupported production.
      diagnose(Tok.getLoc(),
               diag::borrowing_subpattern_unsupported);
      
      // Recover by parsing as if it was supported.
      return parseMatchingPattern(isExprBasic);
    }
    Identifier name;
    SourceLoc nameLoc = consumeIdentifier(name,
                                          /*diagnoseDollarPrefix*/ false);
    auto namedPattern = createBindingFromPattern(nameLoc, name,
                                           VarDecl::Introducer::Borrowing);
    auto bindPattern = new (Context) BindingPattern(
      borrowingLoc, VarDecl::Introducer::Borrowing, namedPattern);
    
    return makeParserResult(bindPattern);
  }

  // matching-pattern ::= 'is' type
  if (Tok.is(tok::kw_is)) {
    SourceLoc isLoc = consumeToken(tok::kw_is);
    ParserResult<TypeRepr> castType = parseType();
    if (castType.isNull() || castType.hasCodeCompletion())
      return nullptr;
    auto *CastTE = new (Context) TypeExpr(castType.get());
    return makeParserResult(new (Context) IsPattern(
        isLoc, CastTE, nullptr, CheckedCastKind::Unresolved));
  }

  // matching-pattern ::= expr
  // Fall back to expression parsing for ambiguous forms. Name lookup will
  // disambiguate.
  ParserResult<Expr> subExpr =
    parseExprImpl(diag::expected_pattern, isExprBasic);
  ParserStatus status = subExpr;
  if (subExpr.isNull())
    return status;

  if (isa<CodeCompletionExpr>(subExpr.get()) && Tok.isFollowingLParen()) {
    // We are in the case like the following of parsing a pattern with the code
    // completion token as base and associated value matches:
    // #^COMPLETE^#(let a)
    // We will have not consumed the `(let a)` in `parseExprPostfixSuffix`
    // because usually suffixes don't influence the code completion's type and
    // the suffix might be unrelated. But the trailing `(let a)` that is left
    // prevents us from forming a valid pattern.
    // Consume and discard the `(let a)`, which just leaves us with the base
    // of the pattern.
    (void)parseExprCallSuffix(subExpr, isExprBasic);
  }

  // The most common case here is to parse something that was a lexically
  // obvious pattern, which will come back wrapped in an immediate
  // UnresolvedPatternExpr.  Transform this now to simplify later code.
  if (auto *UPE = dyn_cast<UnresolvedPatternExpr>(subExpr.get()))
    return makeParserResult(status, UPE->getSubPattern());

  auto *EP = ExprPattern::createParsed(Context, subExpr.get(), CurDeclContext);
  return makeParserResult(status, EP);
}

ParserResult<Pattern>
Parser::parseMatchingPatternAsBinding(PatternBindingState newState,
                                      SourceLoc varLoc, bool isExprBasic) {
  // 'var', 'let', 'inout' patterns shouldn't nest.
  if (InBindingPattern.getIntroducer().has_value()) {
    auto diag = diag::var_pattern_in_var;
    if (Context.LangOpts.hasFeature(Feature::ReferenceBindings))
      diag = diag::var_pattern_in_var_inout;
    diagnose(varLoc, diag,
             *newState.getSelectIndexForIntroducer());
  }

  // 'let' isn't valid inside an implicitly immutable context, but var is.
  if (newState.isLet() &&
      InBindingPattern == PatternBindingState::ImplicitlyImmutable)
    diagnose(varLoc, diag::let_pattern_in_immutable_context);

  // In our recursive parse, remember that we're in a var/let pattern.
  llvm::SaveAndRestore<decltype(InBindingPattern)> T(
      InBindingPattern,
      newState.getPatternBindingStateForIntroducer(VarDecl::Introducer::Var));

  // Reset async attribute in parser context.
  llvm::SaveAndRestore<bool> AsyncAttr(InPatternWithAsyncAttribute, false);

  ParserResult<Pattern> subPattern = parseMatchingPattern(isExprBasic);
  if (subPattern.isNull())
    return nullptr;
  auto *varP = new (Context) BindingPattern(
      varLoc, newState.getIntroducer().value_or(VarDecl::Introducer::Var),
      subPattern.get());
  return makeParserResult(ParserStatus(subPattern), varP);
}

bool Parser::isOnlyStartOfMatchingPattern() {
  if ((Tok.isContextualKeyword("_borrowing")
       || Tok.isContextualKeyword("borrowing"))
      && peekToken().isAny(tok::identifier, tok::kw_self, tok::dollarident,
                           tok::code_complete)
      && !peekToken().isAtStartOfLine()) {
    return true;
  }

  return Tok.isAny(tok::kw_var, tok::kw_let, tok::kw_is) ||
         (Context.LangOpts.hasFeature(Feature::ReferenceBindings) &&
          Tok.isAny(tok::kw_inout));
}


static bool canParsePatternTuple(Parser &P);

///   pattern ::= identifier
///   pattern ::= '_'
///   pattern ::= pattern-tuple
///   pattern ::= 'var' pattern
///   pattern ::= 'let' pattern
static bool canParsePattern(Parser &P) {
  switch (P.Tok.getKind()) {
  case tok::identifier:
  case tok::kw__:
    P.consumeToken();
    return true;
  case tok::kw_inout:
    if (!P.Context.LangOpts.hasFeature(Feature::ReferenceBindings))
      return false;
    LLVM_FALLTHROUGH;
  case tok::kw_let:
  case tok::kw_var:
    P.consumeToken();
    return canParsePattern(P);
  case tok::l_paren:
    return canParsePatternTuple(P);

  default:
    return false;
  }
}


static bool canParsePatternTuple(Parser &P) {
  if (!P.consumeIf(tok::l_paren)) return false;

  if (P.Tok.isNot(tok::r_paren)) {
    do {
      if (!canParsePattern(P)) return false;
    } while (P.consumeIf(tok::comma));
  }

  return P.consumeIf(tok::r_paren);
}

///  typed-pattern ::= pattern (':' type)?
///
bool Parser::canParseTypedPattern() {
  if (!canParsePattern(*this)) return false;
  
  if (consumeIf(tok::colon))
    return canParseType();
  return true;
}