@@ -5574,17 +5574,8 @@ Parser::parseDeclOperator(ParseDeclOptions Flags, DeclAttributes &Attributes) {
5574
5574
5575
5575
Identifier Name = Context.getIdentifier (Tok.getText ());
5576
5576
SourceLoc NameLoc = consumeToken ();
5577
-
5578
- ParserResult<OperatorDecl> Result;
5579
- if (Attributes.hasAttribute <PrefixAttr>())
5580
- Result = parseDeclPrefixOperator (OperatorLoc, Name, NameLoc, Attributes);
5581
- else if (Attributes.hasAttribute <PostfixAttr>())
5582
- Result = parseDeclPostfixOperator (OperatorLoc, Name, NameLoc, Attributes);
5583
- else {
5584
- if (!Attributes.hasAttribute <InfixAttr>())
5585
- diagnose (OperatorLoc, diag::operator_decl_no_fixity);
5586
- Result = parseDeclInfixOperator (OperatorLoc, Name, NameLoc, Attributes);
5587
- }
5577
+
5578
+ auto Result = parseDeclOperatorImpl (OperatorLoc, Name, NameLoc, Attributes);
5588
5579
5589
5580
if (!DCC.movedToTopLevel () && !AllowTopLevel) {
5590
5581
diagnose (OperatorLoc, diag::operator_decl_inner_scope);
@@ -5595,89 +5586,68 @@ Parser::parseDeclOperator(ParseDeclOptions Flags, DeclAttributes &Attributes) {
5595
5586
}
5596
5587
5597
5588
ParserResult<OperatorDecl>
5598
- Parser::parseDeclPrefixOperator (SourceLoc OperatorLoc, Identifier Name,
5589
+ Parser::parseDeclOperatorImpl (SourceLoc OperatorLoc, Identifier Name,
5599
5590
SourceLoc NameLoc, DeclAttributes &Attributes) {
5600
- SourceLoc lBraceLoc;
5601
- if (consumeIf (tok::l_brace, lBraceLoc)) {
5602
- auto Diag = diagnose (lBraceLoc, diag::deprecated_operator_body);
5603
- if (Tok.is (tok::r_brace)) {
5604
- SourceLoc lastGoodLocEnd = Lexer::getLocForEndOfToken (SourceMgr,
5605
- NameLoc);
5606
- SourceLoc rBraceEnd = Lexer::getLocForEndOfToken (SourceMgr, Tok.getLoc ());
5607
- Diag.fixItRemoveChars (lastGoodLocEnd, rBraceEnd);
5608
- }
5609
-
5610
- skipUntilDeclRBrace ();
5611
- (void ) consumeIf (tok::r_brace);
5612
- }
5591
+ bool isPrefix = Attributes.hasAttribute <PrefixAttr>();
5592
+ bool isInfix = Attributes.hasAttribute <InfixAttr>();
5593
+ bool isPostfix = Attributes.hasAttribute <PostfixAttr>();
5613
5594
5614
- auto *Res = new (Context) PrefixOperatorDecl (CurDeclContext, OperatorLoc,
5615
- Name, NameLoc);
5616
- Res->getAttrs () = Attributes;
5617
- return makeParserResult (Res);
5618
- }
5619
-
5620
- ParserResult<OperatorDecl>
5621
- Parser::parseDeclPostfixOperator (SourceLoc OperatorLoc,
5622
- Identifier Name, SourceLoc NameLoc,
5623
- DeclAttributes &Attributes) {
5624
- SourceLoc lBraceLoc;
5625
- if (consumeIf (tok::l_brace, lBraceLoc)) {
5626
- auto Diag = diagnose (lBraceLoc, diag::deprecated_operator_body);
5627
- if (Tok.is (tok::r_brace)) {
5628
- SourceLoc lastGoodLocEnd = Lexer::getLocForEndOfToken (SourceMgr,
5629
- NameLoc);
5630
- SourceLoc rBraceEnd = Lexer::getLocForEndOfToken (SourceMgr, Tok.getLoc ());
5631
- Diag.fixItRemoveChars (lastGoodLocEnd, rBraceEnd);
5632
- }
5633
-
5634
- skipUntilDeclRBrace ();
5635
- (void ) consumeIf (tok::r_brace);
5636
- }
5637
-
5638
- auto Res = new (Context) PostfixOperatorDecl (CurDeclContext, OperatorLoc,
5639
- Name, NameLoc);
5640
- Res->getAttrs () = Attributes;
5641
- return makeParserResult (Res);
5642
- }
5643
-
5644
- ParserResult<OperatorDecl>
5645
- Parser::parseDeclInfixOperator (SourceLoc operatorLoc, Identifier name,
5646
- SourceLoc nameLoc, DeclAttributes &attributes) {
5595
+ // Parse (or diagnose) a specified precedence group.
5647
5596
SourceLoc colonLoc;
5648
5597
Identifier precedenceGroupName;
5649
5598
SourceLoc precedenceGroupNameLoc;
5650
5599
if (consumeIf (tok::colon, colonLoc)) {
5651
5600
if (Tok.is (tok::identifier)) {
5652
5601
precedenceGroupName = Context.getIdentifier (Tok.getText ());
5653
5602
precedenceGroupNameLoc = consumeToken (tok::identifier);
5603
+
5604
+ if (isPrefix || isPostfix)
5605
+ diagnose (colonLoc, diag::precedencegroup_not_infix)
5606
+ .fixItRemove ({colonLoc, precedenceGroupNameLoc});
5654
5607
}
5655
5608
}
5656
-
5609
+
5610
+ // Diagnose deprecated operator body syntax `operator + { ... }`.
5657
5611
SourceLoc lBraceLoc;
5658
5612
if (consumeIf (tok::l_brace, lBraceLoc)) {
5659
- if (Tok.is (tok::r_brace)) {
5660
- SourceLoc lastGoodLoc = precedenceGroupNameLoc;
5661
- if (lastGoodLoc.isInvalid ())
5662
- lastGoodLoc = nameLoc;
5663
- SourceLoc lastGoodLocEnd = Lexer::getLocForEndOfToken (SourceMgr,
5664
- lastGoodLoc);
5665
- SourceLoc rBraceEnd = Lexer::getLocForEndOfToken (SourceMgr, Tok.getLoc ());
5666
- diagnose (lBraceLoc, diag::deprecated_operator_body)
5667
- .fixItRemoveChars (lastGoodLocEnd, rBraceEnd);
5668
- } else {
5613
+ if (isInfix && !Tok.is (tok::r_brace)) {
5669
5614
diagnose (lBraceLoc, diag::deprecated_operator_body_use_group);
5615
+ } else {
5616
+ auto Diag = diagnose (lBraceLoc, diag::deprecated_operator_body);
5617
+ if (Tok.is (tok::r_brace)) {
5618
+ SourceLoc lastGoodLoc = precedenceGroupNameLoc;
5619
+ if (lastGoodLoc.isInvalid ())
5620
+ lastGoodLoc = NameLoc;
5621
+ SourceLoc lastGoodLocEnd = Lexer::getLocForEndOfToken (SourceMgr,
5622
+ lastGoodLoc);
5623
+ SourceLoc rBraceEnd = Lexer::getLocForEndOfToken (SourceMgr, Tok.getLoc ());
5624
+ Diag.fixItRemoveChars (lastGoodLocEnd, rBraceEnd);
5625
+ }
5670
5626
}
5671
5627
5672
5628
skipUntilDeclRBrace ();
5673
5629
(void ) consumeIf (tok::r_brace);
5674
5630
}
5675
-
5676
- auto res = new (Context) InfixOperatorDecl (CurDeclContext, operatorLoc,
5677
- name, nameLoc, colonLoc,
5678
- precedenceGroupName,
5679
- precedenceGroupNameLoc);
5680
- res->getAttrs () = attributes;
5631
+
5632
+
5633
+ OperatorDecl *res;
5634
+ if (Attributes.hasAttribute <PrefixAttr>())
5635
+ res = new (Context) PrefixOperatorDecl (CurDeclContext, OperatorLoc,
5636
+ Name, NameLoc);
5637
+ else if (Attributes.hasAttribute <PostfixAttr>())
5638
+ res = new (Context) PostfixOperatorDecl (CurDeclContext, OperatorLoc,
5639
+ Name, NameLoc);
5640
+ else {
5641
+ if (!Attributes.hasAttribute <InfixAttr>())
5642
+ diagnose (OperatorLoc, diag::operator_decl_no_fixity);
5643
+
5644
+ res = new (Context) InfixOperatorDecl (CurDeclContext, OperatorLoc,
5645
+ Name, NameLoc, colonLoc,
5646
+ precedenceGroupName,
5647
+ precedenceGroupNameLoc);
5648
+ }
5649
+
5650
+ res->getAttrs () = Attributes;
5681
5651
return makeParserResult (res);
5682
5652
}
5683
5653
0 commit comments