Skip to content

Commit 6f243d4

Browse files
committed
AST: Plumb a 'context' parameter through to AbstractFunctionBody's body synthesizer
1 parent 16df434 commit 6f243d4

7 files changed

+40
-33
lines changed

include/swift/AST/Decl.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5194,7 +5194,10 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
51945194
return BodyKind(Bits.AbstractFunctionDecl.BodyKind);
51955195
}
51965196

5197-
using BodySynthesizer = void (*)(AbstractFunctionDecl *);
5197+
struct BodySynthesizer {
5198+
void (* Fn)(AbstractFunctionDecl *, void *);
5199+
void *Context;
5200+
};
51985201

51995202
private:
52005203
ParameterList *Params;
@@ -5318,7 +5321,8 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
53185321
BraceStmt *getBody(bool canSynthesize = true) const {
53195322
if (canSynthesize && getBodyKind() == BodyKind::Synthesize) {
53205323
const_cast<AbstractFunctionDecl *>(this)->setBodyKind(BodyKind::None);
5321-
(*Synthesizer)(const_cast<AbstractFunctionDecl *>(this));
5324+
(Synthesizer.Fn)(const_cast<AbstractFunctionDecl *>(this),
5325+
Synthesizer.Context);
53225326
}
53235327
if (getBodyKind() == BodyKind::Parsed ||
53245328
getBodyKind() == BodyKind::TypeChecked) {
@@ -5350,9 +5354,10 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
53505354
}
53515355

53525356
/// Note that parsing for the body was delayed.
5353-
void setBodySynthesizer(BodySynthesizer synthesizer) {
5357+
void setBodySynthesizer(void (* fn)(AbstractFunctionDecl *, void *),
5358+
void *context = nullptr) {
53545359
assert(getBodyKind() == BodyKind::None);
5355-
Synthesizer = synthesizer;
5360+
Synthesizer = {fn, context};
53565361
setBodyKind(BodyKind::Synthesize);
53575362
}
53585363

lib/Sema/DerivedConformanceCaseIterable.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static bool canDeriveConformance(NominalTypeDecl *type) {
3939
}
4040

4141
/// Derive the implementation of allCases for a "simple" no-payload enum.
42-
void deriveCaseIterable_enum_getter(AbstractFunctionDecl *funcDecl) {
42+
void deriveCaseIterable_enum_getter(AbstractFunctionDecl *funcDecl, void *) {
4343
auto *parentDC = funcDecl->getDeclContext();
4444
auto *parentEnum = parentDC->getSelfEnumDecl();
4545
auto enumTy = parentDC->getDeclaredTypeInContext();

lib/Sema/DerivedConformanceCodable.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ static CallExpr *createContainerKeyedByCall(ASTContext &C, DeclContext *DC,
512512
/// Synthesizes the body for `func encode(to encoder: Encoder) throws`.
513513
///
514514
/// \param encodeDecl The function decl whose body to synthesize.
515-
static void deriveBodyEncodable_encode(AbstractFunctionDecl *encodeDecl) {
515+
static void deriveBodyEncodable_encode(AbstractFunctionDecl *encodeDecl, void *) {
516516
// struct Foo : Codable {
517517
// var x: Int
518518
// var y: String
@@ -749,7 +749,7 @@ static FuncDecl *deriveEncodable_encode(DerivedConformance &derived) {
749749
/// Synthesizes the body for `init(from decoder: Decoder) throws`.
750750
///
751751
/// \param initDecl The function decl whose body to synthesize.
752-
static void deriveBodyDecodable_init(AbstractFunctionDecl *initDecl) {
752+
static void deriveBodyDecodable_init(AbstractFunctionDecl *initDecl, void *) {
753753
// struct Foo : Codable {
754754
// var x: Int
755755
// var y: String
@@ -1024,7 +1024,7 @@ static ValueDecl *deriveDecodable_init(DerivedConformance &derived) {
10241024
/*GenericParams=*/nullptr, conformanceDC);
10251025
initDecl->setImplicit();
10261026
initDecl->setSynthesized();
1027-
initDecl->setBodySynthesizer(deriveBodyDecodable_init);
1027+
initDecl->setBodySynthesizer(&deriveBodyDecodable_init);
10281028

10291029
// This constructor should be marked as `required` for non-final classes.
10301030
if (classDecl && !classDecl->getAttrs().hasAttribute<FinalAttr>()) {

lib/Sema/DerivedConformanceCodingKey.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ using namespace swift;
2929
/// Sets the body of the given function to `return nil`.
3030
///
3131
/// \param funcDecl The function whose body to set.
32-
static void deriveNilReturn(AbstractFunctionDecl *funcDecl) {
32+
static void deriveNilReturn(AbstractFunctionDecl *funcDecl, void *) {
3333
auto *parentDC = funcDecl->getDeclContext();
3434
auto &C = parentDC->getASTContext();
3535

@@ -43,7 +43,7 @@ static void deriveNilReturn(AbstractFunctionDecl *funcDecl) {
4343
/// Sets the body of the given function to `return self.rawValue`.
4444
///
4545
/// \param funcDecl The function whose body to set.
46-
static void deriveRawValueReturn(AbstractFunctionDecl *funcDecl) {
46+
static void deriveRawValueReturn(AbstractFunctionDecl *funcDecl, void *) {
4747
auto *parentDC = funcDecl->getDeclContext();
4848
auto &C = parentDC->getASTContext();
4949

@@ -62,7 +62,7 @@ static void deriveRawValueReturn(AbstractFunctionDecl *funcDecl) {
6262
/// the parameter of the given constructor.
6363
///
6464
/// \param initDecl The constructor whose body to set.
65-
static void deriveRawValueInit(AbstractFunctionDecl *initDecl) {
65+
static void deriveRawValueInit(AbstractFunctionDecl *initDecl, void *) {
6666
auto *parentDC = initDecl->getDeclContext();
6767
auto &C = parentDC->getASTContext();
6868

@@ -190,7 +190,7 @@ static ValueDecl *deriveProperty(DerivedConformance &derived, Type type,
190190
///
191191
/// \param strValDecl The function whose body to set.
192192
static void
193-
deriveBodyCodingKey_enum_stringValue(AbstractFunctionDecl *strValDecl) {
193+
deriveBodyCodingKey_enum_stringValue(AbstractFunctionDecl *strValDecl, void *) {
194194
// enum SomeEnum {
195195
// case A, B, C
196196
// @derived var stringValue: String {
@@ -255,7 +255,7 @@ deriveBodyCodingKey_enum_stringValue(AbstractFunctionDecl *strValDecl) {
255255
///
256256
/// \param initDecl The function whose body to set.
257257
static void
258-
deriveBodyCodingKey_init_stringValue(AbstractFunctionDecl *initDecl) {
258+
deriveBodyCodingKey_init_stringValue(AbstractFunctionDecl *initDecl, void *) {
259259
// enum SomeEnum {
260260
// case A, B, C
261261
// @derived init?(stringValue: String) {
@@ -279,7 +279,7 @@ deriveBodyCodingKey_init_stringValue(AbstractFunctionDecl *initDecl) {
279279

280280
auto elements = enumDecl->getAllElements();
281281
if (elements.empty() /* empty enum */) {
282-
deriveNilReturn(initDecl);
282+
deriveNilReturn(initDecl, nullptr);
283283
return;
284284
}
285285

lib/Sema/DerivedConformanceEquatableHashable.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ static GuardStmt *returnIfNotEqualGuard(ASTContext &C,
303303
}
304304

305305
static void
306-
deriveBodyEquatable_enum_uninhabited_eq(AbstractFunctionDecl *eqDecl) {
306+
deriveBodyEquatable_enum_uninhabited_eq(AbstractFunctionDecl *eqDecl, void *) {
307307
auto parentDC = eqDecl->getDeclContext();
308308
ASTContext &C = parentDC->getASTContext();
309309

@@ -334,7 +334,8 @@ deriveBodyEquatable_enum_uninhabited_eq(AbstractFunctionDecl *eqDecl) {
334334
/// values. This generates code that converts each value to its integer ordinal
335335
/// and compares them, which produces an optimal single icmp instruction.
336336
static void
337-
deriveBodyEquatable_enum_noAssociatedValues_eq(AbstractFunctionDecl *eqDecl) {
337+
deriveBodyEquatable_enum_noAssociatedValues_eq(AbstractFunctionDecl *eqDecl,
338+
void *) {
338339
auto parentDC = eqDecl->getDeclContext();
339340
ASTContext &C = parentDC->getASTContext();
340341

@@ -389,7 +390,8 @@ deriveBodyEquatable_enum_noAssociatedValues_eq(AbstractFunctionDecl *eqDecl) {
389390
/// Derive the body for an '==' operator for an enum where at least one of the
390391
/// cases has associated values.
391392
static void
392-
deriveBodyEquatable_enum_hasAssociatedValues_eq(AbstractFunctionDecl *eqDecl) {
393+
deriveBodyEquatable_enum_hasAssociatedValues_eq(AbstractFunctionDecl *eqDecl,
394+
void *) {
393395
auto parentDC = eqDecl->getDeclContext();
394396
ASTContext &C = parentDC->getASTContext();
395397

@@ -503,7 +505,8 @@ deriveBodyEquatable_enum_hasAssociatedValues_eq(AbstractFunctionDecl *eqDecl) {
503505
}
504506

505507
/// Derive the body for an '==' operator for a struct.
506-
static void deriveBodyEquatable_struct_eq(AbstractFunctionDecl *eqDecl) {
508+
static void deriveBodyEquatable_struct_eq(AbstractFunctionDecl *eqDecl,
509+
void *) {
507510
auto parentDC = eqDecl->getDeclContext();
508511
ASTContext &C = parentDC->getASTContext();
509512

@@ -554,7 +557,7 @@ static void deriveBodyEquatable_struct_eq(AbstractFunctionDecl *eqDecl) {
554557
/// Derive an '==' operator implementation for an enum or a struct.
555558
static ValueDecl *
556559
deriveEquatable_eq(DerivedConformance &derived,
557-
void (*bodySynthesizer)(AbstractFunctionDecl *)) {
560+
void (*bodySynthesizer)(AbstractFunctionDecl *, void *)) {
558561
// enum SomeEnum<T...> {
559562
// case A, B(Int), C(String, Int)
560563
//
@@ -731,7 +734,8 @@ static CallExpr *createHasherCombineCall(ASTContext &C,
731734

732735
static FuncDecl *
733736
deriveHashable_hashInto(DerivedConformance &derived,
734-
void (*bodySynthesizer)(AbstractFunctionDecl *)) {
737+
void (*bodySynthesizer)(AbstractFunctionDecl *,
738+
void *)) {
735739
// @derived func hash(into hasher: inout Hasher)
736740

737741
ASTContext &C = derived.TC.Context;
@@ -792,7 +796,7 @@ deriveHashable_hashInto(DerivedConformance &derived,
792796
/// Derive the body for the hash(into:) method when hashValue has a
793797
/// user-supplied implementation.
794798
static void
795-
deriveBodyHashable_compat_hashInto(AbstractFunctionDecl *hashIntoDecl) {
799+
deriveBodyHashable_compat_hashInto(AbstractFunctionDecl *hashIntoDecl, void *) {
796800
// func hash(into hasher: inout Hasher) {
797801
// hasher.combine(self.hashValue)
798802
// }
@@ -817,8 +821,7 @@ deriveBodyHashable_compat_hashInto(AbstractFunctionDecl *hashIntoDecl) {
817821
/// value.
818822
static void
819823
deriveBodyHashable_enum_rawValue_hashInto(
820-
AbstractFunctionDecl *hashIntoDecl
821-
) {
824+
AbstractFunctionDecl *hashIntoDecl, void *) {
822825
// enum SomeEnum: Int {
823826
// case A, B, C
824827
// @derived func hash(into hasher: inout Hasher) {
@@ -846,8 +849,7 @@ deriveBodyHashable_enum_rawValue_hashInto(
846849
/// values.
847850
static void
848851
deriveBodyHashable_enum_noAssociatedValues_hashInto(
849-
AbstractFunctionDecl *hashIntoDecl
850-
) {
852+
AbstractFunctionDecl *hashIntoDecl, void *) {
851853
// enum SomeEnum {
852854
// case A, B, C
853855
// @derived func hash(into hasher: inout Hasher) {
@@ -888,8 +890,7 @@ deriveBodyHashable_enum_noAssociatedValues_hashInto(
888890
/// values.
889891
static void
890892
deriveBodyHashable_enum_hasAssociatedValues_hashInto(
891-
AbstractFunctionDecl *hashIntoDecl
892-
) {
893+
AbstractFunctionDecl *hashIntoDecl, void *) {
893894
// enum SomeEnumWithAssociatedValues {
894895
// case A, B(Int), C(String, Int)
895896
// @derived func hash(into hasher: inout Hasher) {
@@ -977,7 +978,7 @@ deriveBodyHashable_enum_hasAssociatedValues_hashInto(
977978

978979
/// Derive the body for the 'hash(into:)' method for a struct.
979980
static void
980-
deriveBodyHashable_struct_hashInto(AbstractFunctionDecl *hashIntoDecl) {
981+
deriveBodyHashable_struct_hashInto(AbstractFunctionDecl *hashIntoDecl, void *) {
981982
// struct SomeStruct {
982983
// var x: Int
983984
// var y: String
@@ -1019,7 +1020,7 @@ deriveBodyHashable_struct_hashInto(AbstractFunctionDecl *hashIntoDecl) {
10191020

10201021
/// Derive the body for the 'hashValue' getter.
10211022
static void
1022-
deriveBodyHashable_hashValue(AbstractFunctionDecl *hashValueDecl) {
1023+
deriveBodyHashable_hashValue(AbstractFunctionDecl *hashValueDecl, void *) {
10231024
auto parentDC = hashValueDecl->getDeclContext();
10241025
ASTContext &C = parentDC->getASTContext();
10251026

@@ -1194,7 +1195,7 @@ ValueDecl *DerivedConformance::deriveHashable(ValueDecl *requirement) {
11941195
return nullptr;
11951196

11961197
if (auto ED = dyn_cast<EnumDecl>(Nominal)) {
1197-
void (*bodySynthesizer)(AbstractFunctionDecl *);
1198+
void (*bodySynthesizer)(AbstractFunctionDecl *, void *);
11981199
if (ED->isObjC())
11991200
bodySynthesizer = deriveBodyHashable_enum_rawValue_hashInto;
12001201
else if (ED->hasOnlyCasesWithoutAssociatedValues())

lib/Sema/DerivedConformanceError.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
using namespace swift;
2727

2828
static void deriveBodyBridgedNSError_enum_nsErrorDomain(
29-
AbstractFunctionDecl *domainDecl) {
29+
AbstractFunctionDecl *domainDecl, void *) {
3030
// enum SomeEnum {
3131
// @derived
3232
// static var _nsErrorDomain: String {

lib/Sema/DerivedConformanceRawRepresentable.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ static Type deriveRawRepresentable_Raw(DerivedConformance &derived) {
5959
return derived.getConformanceContext()->mapTypeIntoContext(rawInterfaceType);
6060
}
6161

62-
static void deriveBodyRawRepresentable_raw(AbstractFunctionDecl *toRawDecl) {
62+
static void deriveBodyRawRepresentable_raw(AbstractFunctionDecl *toRawDecl,
63+
void *) {
6364
// enum SomeEnum : SomeType {
6465
// case A = 111, B = 222
6566
// @derived
@@ -188,7 +189,7 @@ static VarDecl *deriveRawRepresentable_raw(DerivedConformance &derived) {
188189
}
189190

190191
static void
191-
deriveBodyRawRepresentable_init(AbstractFunctionDecl *initDecl) {
192+
deriveBodyRawRepresentable_init(AbstractFunctionDecl *initDecl, void *) {
192193
// enum SomeEnum : SomeType {
193194
// case A = 111, B = 222
194195
// @derived

0 commit comments

Comments
 (0)