@@ -82,7 +82,7 @@ static bool canDeriveConformance(DeclContext *DC,
82
82
if (auto enumDecl = dyn_cast<EnumDecl>(target)) {
83
83
// The cases must not have associated values, or all associated values must
84
84
// conform to the protocol.
85
- return allAssociatedValuesConformToProtocol (DC, enumDecl, protocol);
85
+ return DerivedConformance:: allAssociatedValuesConformToProtocol (DC, enumDecl, protocol);
86
86
}
87
87
88
88
if (auto structDecl = dyn_cast<StructDecl>(target)) {
@@ -101,7 +101,7 @@ void diagnoseFailedDerivation(DeclContext *DC, NominalTypeDecl *nominal,
101
101
102
102
if (auto *enumDecl = dyn_cast<EnumDecl>(nominal)) {
103
103
auto nonconformingAssociatedTypes =
104
- associatedValuesNotConformingToProtocol (DC, enumDecl, protocol);
104
+ DerivedConformance:: associatedValuesNotConformingToProtocol (DC, enumDecl, protocol);
105
105
for (auto *typeToDiagnose : nonconformingAssociatedTypes) {
106
106
SourceLoc reprLoc;
107
107
if (auto *repr = typeToDiagnose->getTypeRepr ())
@@ -135,45 +135,6 @@ void diagnoseFailedDerivation(DeclContext *DC, NominalTypeDecl *nominal,
135
135
}
136
136
}
137
137
138
- // / Returns a generated guard statement that checks whether the given lhs and
139
- // / rhs expressions are equal. If not equal, the else block for the guard
140
- // / returns false.
141
- // / \p C The AST context.
142
- // / \p lhsExpr The first expression to compare for equality.
143
- // / \p rhsExpr The second expression to compare for equality.
144
- static GuardStmt *returnIfNotEqualGuard (ASTContext &C,
145
- Expr *lhsExpr,
146
- Expr *rhsExpr) {
147
- SmallVector<StmtConditionElement, 1 > conditions;
148
- SmallVector<ASTNode, 1 > statements;
149
-
150
- // First, generate the statement for the body of the guard.
151
- // return false
152
- auto falseExpr = new (C) BooleanLiteralExpr (false , SourceLoc (),
153
- /* Implicit*/ true );
154
- auto returnStmt = new (C) ReturnStmt (SourceLoc (), falseExpr);
155
- statements.emplace_back (ASTNode (returnStmt));
156
-
157
- // Next, generate the condition being checked.
158
- // lhs == rhs
159
- auto cmpFuncExpr = new (C) UnresolvedDeclRefExpr (
160
- DeclNameRef (C.Id_EqualsOperator ), DeclRefKind::BinaryOperator,
161
- DeclNameLoc ());
162
- auto cmpArgsTuple = TupleExpr::create (C, SourceLoc (),
163
- { lhsExpr, rhsExpr },
164
- { }, { }, SourceLoc (),
165
- /* HasTrailingClosure*/ false ,
166
- /* Implicit*/ true );
167
- auto cmpExpr = new (C) BinaryExpr (cmpFuncExpr, cmpArgsTuple,
168
- /* Implicit*/ true );
169
- conditions.emplace_back (cmpExpr);
170
-
171
- // Build and return the complete guard statement.
172
- // guard lhs == rhs else { return false }
173
- auto body = BraceStmt::create (C, SourceLoc (), statements, SourceLoc ());
174
- return new (C) GuardStmt (SourceLoc (), C.AllocateCopy (conditions), body);
175
- }
176
-
177
138
static std::pair<BraceStmt *, bool >
178
139
deriveBodyEquatable_enum_uninhabited_eq (AbstractFunctionDecl *eqDecl, void *) {
179
140
auto parentDC = eqDecl->getDeclContext ();
@@ -225,9 +186,9 @@ deriveBodyEquatable_enum_noAssociatedValues_eq(AbstractFunctionDecl *eqDecl,
225
186
226
187
// Generate the conversion from the enums to integer indices.
227
188
SmallVector<ASTNode, 6 > statements;
228
- DeclRefExpr *aIndex = convertEnumToIndex (statements, parentDC, enumDecl,
189
+ DeclRefExpr *aIndex = DerivedConformance:: convertEnumToIndex (statements, parentDC, enumDecl,
229
190
aParam, eqDecl, " index_a" );
230
- DeclRefExpr *bIndex = convertEnumToIndex (statements, parentDC, enumDecl,
191
+ DeclRefExpr *bIndex = DerivedConformance:: convertEnumToIndex (statements, parentDC, enumDecl,
231
192
bParam, eqDecl, " index_b" );
232
193
233
194
// Generate the compare of the indices.
@@ -296,7 +257,7 @@ deriveBodyEquatable_enum_hasAssociatedValues_eq(AbstractFunctionDecl *eqDecl,
296
257
297
258
// .<elt>(let l0, let l1, ...)
298
259
SmallVector<VarDecl*, 3 > lhsPayloadVars;
299
- auto lhsSubpattern = enumElementPayloadSubpattern (elt, ' l' , eqDecl,
260
+ auto lhsSubpattern = DerivedConformance:: enumElementPayloadSubpattern (elt, ' l' , eqDecl,
300
261
lhsPayloadVars);
301
262
auto lhsElemPat = new (C) EnumElementPattern (TypeLoc::withoutLoc (enumType),
302
263
SourceLoc (), DeclNameLoc (),
@@ -306,7 +267,7 @@ deriveBodyEquatable_enum_hasAssociatedValues_eq(AbstractFunctionDecl *eqDecl,
306
267
307
268
// .<elt>(let r0, let r1, ...)
308
269
SmallVector<VarDecl*, 3 > rhsPayloadVars;
309
- auto rhsSubpattern = enumElementPayloadSubpattern (elt, ' r' , eqDecl,
270
+ auto rhsSubpattern = DerivedConformance:: enumElementPayloadSubpattern (elt, ' r' , eqDecl,
310
271
rhsPayloadVars);
311
272
auto rhsElemPat = new (C) EnumElementPattern (TypeLoc::withoutLoc (enumType),
312
273
SourceLoc (), DeclNameLoc (),
@@ -352,7 +313,8 @@ deriveBodyEquatable_enum_hasAssociatedValues_eq(AbstractFunctionDecl *eqDecl,
352
313
auto rhsVar = rhsPayloadVars[varIdx];
353
314
auto rhsExpr = new (C) DeclRefExpr (rhsVar, DeclNameLoc (),
354
315
/* Implicit*/ true );
355
- auto guardStmt = returnIfNotEqualGuard (C, lhsExpr, rhsExpr);
316
+ auto guardStmt = DerivedConformance::returnFalseIfNotEqualGuard (C,
317
+ lhsExpr, rhsExpr);
356
318
statementsInCase.emplace_back (guardStmt);
357
319
}
358
320
@@ -438,7 +400,8 @@ deriveBodyEquatable_struct_eq(AbstractFunctionDecl *eqDecl, void *) {
438
400
auto bPropertyExpr = new (C) DotSyntaxCallExpr (bPropertyRef, SourceLoc (),
439
401
bParamRef);
440
402
441
- auto guardStmt = returnIfNotEqualGuard (C, aPropertyExpr, bPropertyExpr);
403
+ auto guardStmt = DerivedConformance::returnFalseIfNotEqualGuard (C,
404
+ aPropertyExpr, bPropertyExpr);
442
405
statements.emplace_back (guardStmt);
443
406
}
444
407
@@ -763,7 +726,7 @@ deriveBodyHashable_enum_noAssociatedValues_hashInto(
763
726
764
727
// generate: switch self {...}
765
728
SmallVector<ASTNode, 3 > stmts;
766
- auto discriminatorExpr = convertEnumToIndex (stmts, parentDC, enumDecl,
729
+ auto discriminatorExpr = DerivedConformance:: convertEnumToIndex (stmts, parentDC, enumDecl,
767
730
selfDecl, hashIntoDecl,
768
731
" discriminator" );
769
732
// generate: hasher.combine(discriminator)
@@ -818,7 +781,7 @@ deriveBodyHashable_enum_hasAssociatedValues_hashInto(
818
781
SmallVector<VarDecl*, 3 > payloadVars;
819
782
SmallVector<ASTNode, 3 > statements;
820
783
821
- auto payloadPattern = enumElementPayloadSubpattern (elt, ' a' , hashIntoDecl,
784
+ auto payloadPattern = DerivedConformance:: enumElementPayloadSubpattern (elt, ' a' , hashIntoDecl,
822
785
payloadVars);
823
786
auto pat = new (C) EnumElementPattern (TypeLoc::withoutLoc (enumType),
824
787
SourceLoc (), DeclNameLoc (),
0 commit comments