@@ -508,25 +508,29 @@ class EnumElementPattern : public Pattern {
508508 DeclNameRef Name;
509509 PointerUnion<EnumElementDecl *, Expr*> ElementDeclOrUnresolvedOriginalExpr;
510510 Pattern /* nullable*/ *SubPattern;
511+ DeclContext *DC;
511512
512513public:
513514 EnumElementPattern (TypeExpr *ParentType, SourceLoc DotLoc,
514515 DeclNameLoc NameLoc, DeclNameRef Name,
515- EnumElementDecl *Element, Pattern *SubPattern)
516+ EnumElementDecl *Element, Pattern *SubPattern,
517+ DeclContext *DC)
516518 : Pattern(PatternKind::EnumElement), ParentType(ParentType),
517519 DotLoc (DotLoc), NameLoc(NameLoc), Name(Name),
518- ElementDeclOrUnresolvedOriginalExpr(Element), SubPattern(SubPattern) {
520+ ElementDeclOrUnresolvedOriginalExpr(Element), SubPattern(SubPattern),
521+ DC(DC) {
519522 assert (ParentType && " Missing parent type?" );
520523 }
521524
522525 // / Create an unresolved EnumElementPattern for a `.foo` pattern relying on
523526 // / contextual type.
524527 EnumElementPattern (SourceLoc DotLoc, DeclNameLoc NameLoc, DeclNameRef Name,
525- Pattern *SubPattern, Expr *UnresolvedOriginalExpr)
528+ Pattern *SubPattern, Expr *UnresolvedOriginalExpr,
529+ DeclContext *DC)
526530 : Pattern(PatternKind::EnumElement), ParentType(nullptr ), DotLoc(DotLoc),
527531 NameLoc(NameLoc), Name(Name),
528532 ElementDeclOrUnresolvedOriginalExpr(UnresolvedOriginalExpr),
529- SubPattern(SubPattern) {}
533+ SubPattern(SubPattern), DC(DC) {}
530534
531535 bool hasSubPattern () const { return SubPattern; }
532536
@@ -540,6 +544,8 @@ class EnumElementPattern : public Pattern {
540544
541545 void setSubPattern (Pattern *p) { SubPattern = p; }
542546
547+ DeclContext *getDeclContext () const { return DC; }
548+
543549 DeclNameRef getName () const { return Name; }
544550
545551 EnumElementDecl *getElementDecl () const {
@@ -647,43 +653,59 @@ class OptionalSomePattern : public Pattern {
647653class ExprPattern : public Pattern {
648654 llvm::PointerIntPair<Expr *, 1 , bool > SubExprAndIsResolved;
649655
650- // / An expression constructed during type-checking that produces a call to the
651- // / '~=' operator comparing the match expression on the left to the matched
652- // / value on the right.
653- Expr *MatchExpr = nullptr ;
656+ DeclContext *DC;
657+
658+ // / A synthesized call to the '~=' operator comparing the match expression
659+ // / on the left to the matched value on the right.
660+ mutable Expr *MatchExpr = nullptr ;
661+
662+ // / An implicit variable used to represent the RHS value of the synthesized
663+ // / match expression.
664+ mutable VarDecl *MatchVar = nullptr ;
654665
655- // / An implicit variable used to represent the RHS value of the match.
656- VarDecl *MatchVar = nullptr ;
666+ ExprPattern (Expr *E, DeclContext *DC, bool isResolved)
667+ : Pattern(PatternKind::Expr), SubExprAndIsResolved(E, isResolved),
668+ DC (DC) {}
657669
658- ExprPattern (Expr *E, bool isResolved)
659- : Pattern(PatternKind::Expr), SubExprAndIsResolved(E, isResolved) {}
670+ friend class ExprPatternMatchRequest ;
660671
661672public:
662673 // / Create a new parsed unresolved ExprPattern.
663- static ExprPattern *createParsed (ASTContext &ctx, Expr *E);
674+ static ExprPattern *createParsed (ASTContext &ctx, Expr *E, DeclContext *DC );
664675
665676 // / Create a new resolved ExprPattern. This should be used in cases
666677 // / where a user-written expression should be treated as an ExprPattern.
667- static ExprPattern *createResolved (ASTContext &ctx, Expr *E);
678+ static ExprPattern *createResolved (ASTContext &ctx, Expr *E, DeclContext *DC );
668679
669680 // / Create a new implicit resolved ExprPattern.
670- static ExprPattern *createImplicit (ASTContext &ctx, Expr *E);
681+ static ExprPattern *createImplicit (ASTContext &ctx, Expr *E, DeclContext *DC );
671682
672683 Expr *getSubExpr () const { return SubExprAndIsResolved.getPointer (); }
673684 void setSubExpr (Expr *e) { SubExprAndIsResolved.setPointer (e); }
674685
675- Expr *getMatchExpr () const { return MatchExpr; }
686+ DeclContext *getDeclContext () const { return DC; }
687+
688+ // / The match expression if it has been computed, \c nullptr otherwise.
689+ // / Should only be used by the ASTDumper and ASTWalker.
690+ Expr *getCachedMatchExpr () const { return MatchExpr; }
691+
692+ // / The match variable if it has been computed, \c nullptr otherwise.
693+ // / Should only be used by the ASTDumper and ASTWalker.
694+ VarDecl *getCachedMatchVar () const { return MatchVar; }
695+
696+ // / A synthesized call to the '~=' operator comparing the match expression
697+ // / on the left to the matched value on the right.
698+ Expr *getMatchExpr () const ;
699+
700+ // / An implicit variable used to represent the RHS value of the synthesized
701+ // / match expression.
702+ VarDecl *getMatchVar () const ;
703+
676704 void setMatchExpr (Expr *e) {
677- assert (isResolved () && " cannot set match fn for unresolved expr patter " );
705+ assert (MatchExpr && " Should only update an existing MatchExpr " );
678706 MatchExpr = e;
679707 }
680708
681- VarDecl *getMatchVar () const { return MatchVar; }
682- void setMatchVar (VarDecl *v) {
683- assert (isResolved () && " cannot set match var for unresolved expr patter" );
684- MatchVar = v;
685- }
686-
687709 SourceLoc getLoc () const ;
688710 SourceRange getSourceRange () const ;
689711
@@ -851,6 +873,9 @@ class ContextualPattern {
851873};
852874
853875void simple_display (llvm::raw_ostream &out, const ContextualPattern &pattern);
876+ void simple_display (llvm::raw_ostream &out, const Pattern *pattern);
877+
878+ SourceLoc extractNearestSourceLoc (const Pattern *pattern);
854879
855880} // end namespace swift
856881
0 commit comments