|
16 | 16 |
|
17 | 17 | #include "swift/AST/Pattern.h" |
18 | 18 | #include "swift/AST/ASTContext.h" |
| 19 | +#include "swift/AST/ASTVisitor.h" |
19 | 20 | #include "swift/AST/ASTWalker.h" |
20 | 21 | #include "swift/AST/Expr.h" |
21 | 22 | #include "swift/AST/GenericEnvironment.h" |
@@ -569,6 +570,49 @@ VarDecl *ExprPattern::getMatchVar() const { |
569 | 570 | .getMatchVar(); |
570 | 571 | } |
571 | 572 |
|
| 573 | +void ExprPattern::updateMatchExpr(Expr *e) const { |
| 574 | + class FindMatchOperatorDeclRef: public ASTWalker { |
| 575 | + public: |
| 576 | + ValueOwnership Ownership = ValueOwnership::Default; |
| 577 | + |
| 578 | + PreWalkResult<Expr *> walkToExprPre(Expr *E) override { |
| 579 | + // See if this is the reference to the ~= operator used. |
| 580 | + auto declRef = dyn_cast<DeclRefExpr>(E); |
| 581 | + if (!declRef) { |
| 582 | + return Action::Continue(E); |
| 583 | + } |
| 584 | + auto decl = declRef->getDecl(); |
| 585 | + auto declName = decl->getName(); |
| 586 | + if (!declName.isOperator()) { |
| 587 | + return Action::Continue(E); |
| 588 | + } |
| 589 | + |
| 590 | + if (!declName.getBaseIdentifier().is("~=")) { |
| 591 | + return Action::Continue(E); |
| 592 | + } |
| 593 | + |
| 594 | + // We found a `~=` declref. Get the value ownership from the parameter. |
| 595 | + auto fnTy = decl->getInterfaceType()->castTo<AnyFunctionType>(); |
| 596 | + if (decl->isStatic()) { |
| 597 | + fnTy = fnTy->getResult()->castTo<AnyFunctionType>(); |
| 598 | + } |
| 599 | + // Subject value is the right-hand operand to the operator. |
| 600 | + assert(fnTy->getParams().size() == 2); |
| 601 | + Ownership = fnTy->getParams()[1].getValueOwnership(); |
| 602 | + // Operators are always normal functions or methods, so their default |
| 603 | + // parameter ownership is always borrowing. |
| 604 | + if (Ownership == ValueOwnership::Default) { |
| 605 | + Ownership = ValueOwnership::Shared; |
| 606 | + } |
| 607 | + return Action::Stop(); |
| 608 | + } |
| 609 | + }; |
| 610 | + FindMatchOperatorDeclRef walker; |
| 611 | + e->walk(walker); |
| 612 | + |
| 613 | + MatchExprAndOperandOwnership = {e, walker.Ownership}; |
| 614 | +} |
| 615 | + |
572 | 616 | SourceLoc EnumElementPattern::getStartLoc() const { |
573 | 617 | return (ParentType && !ParentType->isImplicit()) |
574 | 618 | ? ParentType->getSourceRange().Start |
@@ -680,3 +724,89 @@ void swift::simple_display(llvm::raw_ostream &out, const Pattern *pattern) { |
680 | 724 | SourceLoc swift::extractNearestSourceLoc(const Pattern *pattern) { |
681 | 725 | return pattern->getLoc(); |
682 | 726 | } |
| 727 | + |
| 728 | +ValueOwnership |
| 729 | +Pattern::getOwnership( |
| 730 | + SmallVectorImpl<Pattern *> *mostRestrictiveSubpatterns) const |
| 731 | +{ |
| 732 | + class GetPatternOwnership: public PatternVisitor<GetPatternOwnership, void> { |
| 733 | + public: |
| 734 | + ValueOwnership Ownership = ValueOwnership::Shared; |
| 735 | + SmallVectorImpl<Pattern *> *RestrictingPatterns = nullptr; |
| 736 | + |
| 737 | + void increaseOwnership(ValueOwnership newOwnership, Pattern *p) { |
| 738 | + // If the new ownership is stricter than the current ownership, then |
| 739 | + // clear the restricting patterns we'd collected and start over with the |
| 740 | + // new stricter ownership. |
| 741 | + if (newOwnership > Ownership) { |
| 742 | + Ownership = newOwnership; |
| 743 | + if (RestrictingPatterns) { |
| 744 | + RestrictingPatterns->clear(); |
| 745 | + } |
| 746 | + } |
| 747 | + |
| 748 | + if (RestrictingPatterns |
| 749 | + && newOwnership == Ownership |
| 750 | + && Ownership > ValueOwnership::Shared) { |
| 751 | + RestrictingPatterns->push_back(p); |
| 752 | + } |
| 753 | + } |
| 754 | + |
| 755 | +#define USE_SUBPATTERN(Kind) \ |
| 756 | + void visit##Kind##Pattern(Kind##Pattern *pattern) { \ |
| 757 | + return visit(pattern->getSubPattern()); \ |
| 758 | + } |
| 759 | + |
| 760 | + USE_SUBPATTERN(Paren) |
| 761 | + USE_SUBPATTERN(Typed) |
| 762 | + USE_SUBPATTERN(Binding) |
| 763 | +#undef USE_SUBPATTERN |
| 764 | + void visitTuplePattern(TuplePattern *p) { |
| 765 | + for (auto &element : p->getElements()) { |
| 766 | + visit(element.getPattern()); |
| 767 | + } |
| 768 | + } |
| 769 | + |
| 770 | + void visitNamedPattern(NamedPattern *p) { |
| 771 | + // `var` and `let` bindings consume the matched value. |
| 772 | + // TODO: borrowing/mutating/consuming parameters |
| 773 | + increaseOwnership(ValueOwnership::Owned, p); |
| 774 | + } |
| 775 | + |
| 776 | + void visitAnyPattern(AnyPattern *p) { |
| 777 | + /* no change */ |
| 778 | + } |
| 779 | + void visitBoolPattern(BoolPattern *p) { |
| 780 | + /* no change */ |
| 781 | + } |
| 782 | + |
| 783 | + void visitIsPattern(IsPattern *p) { |
| 784 | + // Casting currently always consumes. |
| 785 | + // TODO: Sometimes maybe it doesn't need to be. |
| 786 | + increaseOwnership(ValueOwnership::Owned, p); |
| 787 | + } |
| 788 | + |
| 789 | + void visitEnumElementPattern(EnumElementPattern *p) { |
| 790 | + if (p->hasSubPattern()) { |
| 791 | + visit(p->getSubPattern()); |
| 792 | + } |
| 793 | + } |
| 794 | + |
| 795 | + void visitOptionalSomePattern(OptionalSomePattern *p) { |
| 796 | + visit(p->getSubPattern()); |
| 797 | + } |
| 798 | + |
| 799 | + void visitExprPattern(ExprPattern *p) { |
| 800 | + // We can't get the ownership reliably if the pattern hasn't been resolved. |
| 801 | + if (!p->isResolved()) { |
| 802 | + return; |
| 803 | + } |
| 804 | + increaseOwnership(p->getMatchOperandOwnership(), p); |
| 805 | + } |
| 806 | + }; |
| 807 | + |
| 808 | + GetPatternOwnership visitor; |
| 809 | + visitor.RestrictingPatterns = mostRestrictiveSubpatterns; |
| 810 | + visitor.visit(const_cast<Pattern *>(this)); |
| 811 | + return visitor.Ownership; |
| 812 | +} |
0 commit comments