Skip to content

Commit 9b8bc60

Browse files
committed
[AST] NFC: Refactor ExprPattern construction
Introduce 3 separate factory constructors, and remove the ability to specify the match expr and var, as those should be synthesized on-demand.
1 parent 3a7b558 commit 9b8bc60

8 files changed

+37
-35
lines changed

include/swift/AST/Pattern.h

+14-15
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,6 @@ class OptionalSomePattern : public Pattern {
639639
}
640640
};
641641

642-
643642
/// A pattern which matches a value obtained by evaluating an expression.
644643
/// The match will be tested using user-defined '~=' operator function lookup;
645644
/// the match succeeds if 'patternValue ~= matchedValue' produces a true value.
@@ -649,24 +648,24 @@ class ExprPattern : public Pattern {
649648
/// An expression constructed during type-checking that produces a call to the
650649
/// '~=' operator comparing the match expression on the left to the matched
651650
/// value on the right.
652-
Expr *MatchExpr;
651+
Expr *MatchExpr = nullptr;
653652

654653
/// An implicit variable used to represent the RHS value of the match.
655-
VarDecl *MatchVar;
654+
VarDecl *MatchVar = nullptr;
655+
656+
ExprPattern(Expr *E, bool isResolved)
657+
: Pattern(PatternKind::Expr), SubExprAndIsResolved(E, isResolved) {}
656658

657659
public:
658-
/// Construct an ExprPattern.
659-
ExprPattern(Expr *e, bool isResolved, Expr *matchExpr, VarDecl *matchVar);
660-
661-
/// Construct an unresolved ExprPattern.
662-
ExprPattern(Expr *e)
663-
: ExprPattern(e, false, nullptr, nullptr)
664-
{}
665-
666-
/// Construct a resolved ExprPattern.
667-
ExprPattern(Expr *e, Expr *matchExpr, VarDecl *matchVar)
668-
: ExprPattern(e, true, matchExpr, matchVar)
669-
{}
660+
/// Create a new parsed unresolved ExprPattern.
661+
static ExprPattern *createParsed(ASTContext &ctx, Expr *E);
662+
663+
/// Create a new resolved ExprPattern. This should be used in cases
664+
/// where a user-written expression should be treated as an ExprPattern.
665+
static ExprPattern *createResolved(ASTContext &ctx, Expr *E);
666+
667+
/// Create a new implicit resolved ExprPattern.
668+
static ExprPattern *createImplicit(ASTContext &ctx, Expr *E);
670669

671670
Expr *getSubExpr() const { return SubExprAndIsResolved.getPointer(); }
672671
void setSubExpr(Expr *e) { SubExprAndIsResolved.setPointer(e); }

lib/AST/Pattern.cpp

+12-6
Original file line numberDiff line numberDiff line change
@@ -501,12 +501,18 @@ void IsPattern::setCastType(Type type) {
501501

502502
TypeRepr *IsPattern::getCastTypeRepr() const { return CastType->getTypeRepr(); }
503503

504-
/// Construct an ExprPattern.
505-
ExprPattern::ExprPattern(Expr *e, bool isResolved, Expr *matchExpr,
506-
VarDecl *matchVar)
507-
: Pattern(PatternKind::Expr), SubExprAndIsResolved(e, isResolved),
508-
MatchExpr(matchExpr), MatchVar(matchVar) {
509-
assert(!matchExpr || e->isImplicit() == matchExpr->isImplicit());
504+
ExprPattern *ExprPattern::createParsed(ASTContext &ctx, Expr *E) {
505+
return new (ctx) ExprPattern(E, /*isResolved*/ false);
506+
}
507+
508+
ExprPattern *ExprPattern::createResolved(ASTContext &ctx, Expr *E) {
509+
return new (ctx) ExprPattern(E, /*isResolved*/ true);
510+
}
511+
512+
ExprPattern *ExprPattern::createImplicit(ASTContext &ctx, Expr *E) {
513+
auto *EP = ExprPattern::createResolved(ctx, E);
514+
EP->setImplicit();
515+
return EP;
510516
}
511517

512518
SourceLoc EnumElementPattern::getStartLoc() const {

lib/Parse/ParsePattern.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -1314,8 +1314,9 @@ ParserResult<Pattern> Parser::parseMatchingPattern(bool isExprBasic) {
13141314
// UnresolvedPatternExpr. Transform this now to simplify later code.
13151315
if (auto *UPE = dyn_cast<UnresolvedPatternExpr>(subExpr.get()))
13161316
return makeParserResult(status, UPE->getSubPattern());
1317-
1318-
return makeParserResult(status, new (Context) ExprPattern(subExpr.get()));
1317+
1318+
auto *EP = ExprPattern::createParsed(Context, subExpr.get());
1319+
return makeParserResult(status, EP);
13191320
}
13201321

13211322
ParserResult<Pattern> Parser::parseMatchingPatternAsLetOrVar(bool isLet,

lib/Parse/ParseStmt.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,7 @@ static void parseGuardedPattern(Parser &P, GuardedPattern &result,
10611061
// Do some special-case code completion for the start of the pattern.
10621062
if (P.Tok.is(tok::code_complete)) {
10631063
auto CCE = new (P.Context) CodeCompletionExpr(P.Tok.getLoc());
1064-
result.ThePattern = new (P.Context) ExprPattern(CCE);
1064+
result.ThePattern = ExprPattern::createParsed(P.Context, CCE);
10651065
if (P.IDECallbacks) {
10661066
switch (parsingContext) {
10671067
case GuardedPatternContext::Case:

lib/Sema/CSSimplify.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -9847,8 +9847,8 @@ static bool inferEnumMemberThroughTildeEqualsOperator(
98479847

98489848
// Slots for expression and variable are going to be filled via
98499849
// synthesizing ~= operator application.
9850-
auto *EP = new (ctx) ExprPattern(pattern->getUnresolvedOriginalExpr(),
9851-
/*matchExpr=*/nullptr, /*matchVar=*/nullptr);
9850+
auto *EP =
9851+
ExprPattern::createResolved(ctx, pattern->getUnresolvedOriginalExpr());
98529852

98539853
auto tildeEqualsApplication =
98549854
TypeChecker::synthesizeTildeEqualsOperatorApplication(EP, DC, enumTy);

lib/Sema/DerivedConformanceCodingKey.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,7 @@ deriveBodyCodingKey_init_stringValue(AbstractFunctionDecl *initDecl, void *) {
277277
for (auto *elt : elements) {
278278
auto *litExpr = new (C) StringLiteralExpr(elt->getNameStr(), SourceRange(),
279279
/*Implicit=*/true);
280-
auto *litPat = new (C) ExprPattern(litExpr, /*IsResolved=*/true, nullptr,
281-
nullptr);
282-
litPat->setImplicit();
280+
auto *litPat = ExprPattern::createImplicit(C, litExpr);
283281

284282
auto labelItem = CaseLabelItem(litPat);
285283

lib/Sema/DerivedConformanceRawRepresentable.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,7 @@ deriveBodyRawRepresentable_init(AbstractFunctionDecl *initDecl, void *) {
323323
stringExprs.push_back(litExpr);
324324
litExpr = IntegerLiteralExpr::createFromUnsigned(C, Idx, SourceLoc());
325325
}
326-
auto litPat = new (C) ExprPattern(litExpr, /*isResolved*/ true,
327-
nullptr, nullptr);
328-
litPat->setImplicit();
326+
auto *litPat = ExprPattern::createImplicit(C, litExpr);
329327

330328
/// Statements in the body of this case.
331329
SmallVector<ASTNode, 2> stmts;

lib/Sema/TypeCheckPattern.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ class ResolvePattern : public ASTVisitor<ResolvePattern,
244244
if (Pattern *p = visit(E))
245245
return p;
246246

247-
return new (Context) ExprPattern(E, nullptr, nullptr);
247+
return ExprPattern::createResolved(Context, E);
248248
}
249249

250250
/// Turn an argument list into a matching tuple or paren pattern.
@@ -1427,8 +1427,8 @@ Pattern *TypeChecker::coercePatternToType(ContextualPattern pattern,
14271427
// If we have the original expression parse tree, try reinterpreting
14281428
// it as an expr-pattern if enum element lookup failed, since `.foo`
14291429
// could also refer to a static member of the context type.
1430-
P = new (Context) ExprPattern(EEP->getUnresolvedOriginalExpr(),
1431-
nullptr, nullptr);
1430+
P = ExprPattern::createResolved(Context,
1431+
EEP->getUnresolvedOriginalExpr());
14321432
return coercePatternToType(
14331433
pattern.forSubPattern(P, /*retainTopLevel=*/true), type,
14341434
options);

0 commit comments

Comments
 (0)